Reputation: 79
have the following code for retrieving the stock name and its ltp.
df=pd.DataFrame(data=None)
def on_ticks(ws, ticks):
global df
for sc in ticks:
token=sc['instrument_token']
name=trd_portfolio[token]['name']
ltp=sc['last_price']
df=df.append([name,ltp],ignore_index=True)
print(df)
However, both the appended items, ie. name and ltp are getting extracted in the same column due to which am unable to manipulate the data further. Same of the output is as below
0
0 BANKBARODA
1 39.05
2 NATCOPHARM
3 574.55
4 AUROPHARMA
...
4249 194.15
4250 FRETAIL
4251 80.9
4252 HDFCLIFE
4253 517.95
[4254 rows x 1 columns]
please suggest a way so that i can have the name and ltp in two different columns for further working.
Upvotes: 1
Views: 91
Reputation: 1413
# creating a new dataframe with name and ltp values
# however, since the requirement is to have these within individual
# columns and not rows, using transpose (T) to address it
df1 = pd.DataFrame([name,ltp]).T
# explicitly adding column names to the temp dataframe and the same will repeat
# everytime a new temp dataframe is formed. This will ensure that the new values
# get appended to the correct columns in the aggregated version
df1.columns = ['name', 'ltp']
# append the temp dataframe to the aggregated version to be published as final
df=df.append(df1,ignore_index=True)
Upvotes: 0
Reputation: 14181
Use double braces:
df = df.append([[name, ltp]], ignore_index=True)
Upvotes: 0