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