Append/Replace a row in a pandas DataFrame
Paulo da Silva
p_s_d_a_s_i_l_v_a_ns at netcabo.pt
Wed Apr 13 16:10:25 EDT 2016
Hi all.
I am learning pandas DataFrame and I want to add (eventually replace by
index) some rows.
For adding here is what I tried:
>df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
>df
A B C D
2013年01月01日 -0.111621 1.126761 -2.420517 0.660948
2013年01月02日 -0.243397 -0.975684 -0.679209 -0.656913
2013年01月03日 0.405816 0.478353 0.621906 -0.262615
2013年01月04日 -0.380249 0.416711 -0.906286 1.828339
2013年01月05日 0.772747 0.993784 0.452746 1.665306
2013年01月06日 0.535011 -0.662874 1.504281 0.543537
[6 rows x 4 columns]
>dft=pd.DataFrame([[1,2,3,4]],
index=[datetime.date(2016,1,12)],columns=df.columns)
>dft
A B C D
2016年01月12日 1 2 3 4
[1 rows x 4 columns]
>pd.concat([df,dft])
Out[71]:
A B C D
2013年01月01日 00:00:00 -0.111621 1.126761 -2.420517 0.660948
2013年01月02日 00:00:00 -0.243397 -0.975684 -0.679209 -0.656913
2013年01月03日 00:00:00 0.405816 0.478353 0.621906 -0.262615
2013年01月04日 00:00:00 -0.380249 0.416711 -0.906286 1.828339
2013年01月05日 00:00:00 0.772747 0.993784 0.452746 1.665306
2013年01月06日 00:00:00 0.535011 -0.662874 1.504281 0.543537
2016年01月12日 1.000000 2.000000 3.000000 4.000000
[7 rows x 4 columns]
Why am I getting the second column?!
How do I do to have a row replaced instead of added if its date (index)
is an existent one?
Thanks for any help or comments.
Paulo
More information about the Python-list
mailing list