简体   繁体   中英

Add new row to time series DataFrame

I have a DataFrame loaded from a CSV file which I would like to append with a new row of values and then save it back to CSV. However, there are a few characteristics to this DataFrame:

  • Its columns are not all of the same time, some are strings, some are floats (which makes this method not work for me;
  • Its index is a datetime format which only needs to register the date, so whenever using df.loc[datetime] = [value1, value2, .., value_n] (as mentioned here ), if the index already exists in my DataFrame, all rows containing the same date will be updated with the inputs;

One solution I managed to come up with was to create a new 1-row DataFrame from a dict using the original's columns as keys, so I could then apply pd.concat to add the new row, but I am wondering if there is a simpler and more elegant way to do this?

you can append 1 row, with or without the index:

df = df.append(pd.DataFrame({'value_1':value_1,'value_n':value_n},index=this_index))

df = df.append({'value_1':value_1,'value_n':value_n},ignore_index=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM