JavaScript
x
4
1
print(stocksList.tail(1))
2
stocksList.loc[len(stocksList.index)] = ["NSEI"]
3
print(stocksList.tail(1))
4
Above code prints same value twice i.e.
JavaScript
1
6
1
Symbol
2
1684 ZUARIGLOB
3
4
Symbol
5
1684 ZUARIGLOB
6
Why is it not appending NSEI at the end of the stocksList dataframe?
Full code:
JavaScript
1
10
10
1
folPath = "D:\MyDocs\STKS\YT\"
2
3
nifty50 = pd.read_csv(folPath + "n50.csv")
4
stocksList = pd.read_csv(folPath + "stocksList.csv")
5
stocksList = stocksList[~stocksList['Symbol'].isin(nifty50['Symbol'])]
6
print(stocksList.tail(1))
7
stocksList.loc[len(stocksList), 'Symbol'] = "NSEI"
8
print(stocksList.tail(1))
9
print(stocksList)
10
Advertisement
Answer
how your code is flawed
Relying on the length of the index on a dataframe with a reworked index is not reliable. Here is a simple example demonstrating how it can fail.
input:
JavaScript
1
3
1
df = pd.DataFrame({'Symbol': list('ABCD')},
2
index=np.arange(4))
3
JavaScript
1
6
1
Symbol
2
0 A
3
1 B
4
2 C
5
3 D
6
Pre-processing:
JavaScript
1
7
1
>>> bad_symbols = ['A', 'B']
2
>>> df = df[~df['Symbol'].isin(bad_symbols)]
3
>>> df
4
Symbol
5
2 C
6
3 D
7
Attempt to append a row at the end using index length:
JavaScript
1
6
1
>>> df.loc[len(df.index), 'Symbol'] = 'E'
2
>>> df
3
Symbol
4
2 E
5
3 D
6
See what happended here? len(df.index)
is 2
, but 2
is an already existing row.
how to fix it
Use a reliable method to append a new row. Let’s start again from:
JavaScript
1
4
1
Symbol
2
2 C
3
3 D
4
JavaScript
1
7
1
>>> df = df.append(pd.Series({'Symbol': 'E'}, name=max(df.index)+1))
2
>>> df
3
Symbol
4
2 C
5
3 D
6
4 E
7
Or, aternatively:
JavaScript
1
2
1
df.loc[max(df.index)+1, 'Symbol'] = 'E'
2
but be careful of SettingWithCopyWarning