0

I was working on this question filter iteration using FOR. I wonder how to replace the last cell of year column in each csv file generated. Lets say I want to replace the last cell (of column year) of each cvs file by current year (2018). I did the following code:

for i, g in df.sort_values("year").groupby("Univers", sort=False):
    for y in g.iloc[-1, g.columns.get_loc('year')]:
        y = 2018
    g.to_csv('{}.xls'.format(i))

But I get the same column with any changes. Any ideas how to do this?

Newbie
  • 353
  • 2
  • 12

1 Answers1

0

The problem seems to be of two fold: first find the index of the last row and then replace it at (last_row_idx, "year").

Try this

for i, g in df.sort_values("year").groupby("Univers", sort=False):
    last_row_idx = g.tail(1).index.item()  # first task: find index
    g.at[last_row_idx, "year"] = 2018      # replace
    g.to_csv('{}.xls'.format(i))

Alternatively, one can also use g.set_value(last_row_idx, "year", 2018) to set value at a particular cell.

Reference

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_value.html

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.at.html

Set value for particular cell in pandas DataFrame

Get index of a row of a pandas dataframe as an integer

Tai
  • 6,494
  • 3
  • 23
  • 40