1

I want to update last row of DataFrame.
But, what is the best way to update it?

I just want to update like following data.
Any suggestion?

time A B C
2019-01-01 1 2 3
2019-01-02 4 5 6
2019-01-03 7 8 9

time A B C
2019-01-01 1 2 3
2019-01-02 4 5 6
2019-01-03 10 11 12
naohide_a
  • 1,116
  • 2
  • 13
  • 30

1 Answers1

4

I think the simpliest is assign list to last row with same number of values like number of columns:

df.iloc[-1] = [10,11,12]
print (df)
             A   B   C
time                  
2019-01-01   1   2   3
2019-01-02   4   5   6
2019-01-03  10  11  12

If want assign Series need index same like columns of updated DataFrame:

s = pd.Series([10,11,12])

s.index = df.columns
df.iloc[-1] = s
print (df)
             A   B   C
time                  
2019-01-01   1   2   3
2019-01-02   4   5   6
2019-01-03  10  11  12
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I tried. But, I received this warning. What's wrong? SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy – naohide_a Jun 17 '19 at 02:01
  • @naohide - I think need `copy` here, check [this](https://stackoverflow.com/a/53954986) – jezrael Jun 17 '19 at 05:08
  • 1
    really, thank you :) – naohide_a Jun 17 '19 at 06:01