python - Setting With Enlargement: How to add one row DataFrame to another DataFrame -


i want create empty dataframe append others single row dataframe new data. trying use panda's "setting enlargement" efficient appending.

import numpy np import pandas pd datetime import datetime pandas import dataframe  df = dataframe(columns=["open","high","low","close","volume","open_interest"])  row_one = dataframe({"open":10,"high":11,"low":9,"close":10,"volume":100,"open_interest":np.nan}, index = [datetime(2017,1,1)]) row_two = dataframe({"open":9,"high":12,"low":8,"close":10.50,"volume":500,"open_interest":np.nan}, index = [datetime(2017,1,2)]) 

now, when try append new row following setting enlargement rules:

df[row_one.index] = row_one.columns 

i error:

"datetimeindex(['2017-01-01'], dtype='datetime64[ns]', freq=none) not in index" 

i thought row should automatically added because not in dataframe. doing wrong?

you need loc setting enlargement, select index value [0] scalar , last 'convert' row_one series selecting via iloc:

df.loc[row_one.index[0]] = row_one.iloc[0] print (df)             open  high  low  close  volume  open_interest 2017-01-01  10.0  11.0  9.0   10.0   100.0            nan 

but better use concat, if multiple dfs:

df = pd.concat([row_one, row_two]) 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -