Reputation: 551
I need to retrieve the last row of a CSV file, and append it onto the end to extend it any n number of times. I attempted to do this with Pandas:
df = pd.read_csv('file.csv')
last_row = df.iloc[-1]
last_row.to_csv('file.csv', mode='a', header=False)
However, this appends the last row as a single column into the CSV file. Is there a way to properly append the last row column-wise into the CSV file, preferably using Pandas?
Upvotes: 3
Views: 1210
Reputation: 551
Answering my own question:
I have figured out how to maintain the order of columns and append to the end of the existing dataframe. It is worth noting that pd.tail()
does not maintain the order of columns.
df = pd.read_csv('file.csv')
df_fluxes = df.iloc[[-1] * n]
df_fluxes.to_csv(run_dir+'em_line_fluxes.csv', mode='a', header=False, index=False)
Upvotes: 0
Reputation: 23099
IIUC, you can use tail
and concat
to extend your dataframe.
df = pd.read_csv('file.csv')
last_row = df.tail(1)
last_row = pd.concat([last_row] * n)
last_row.to_csv('file.csv', mode='a', header=False)
Upvotes: 1