Reputation: 89
I am struggling to find the correct arguments to use np.savetxt() for my dataframe. I don't want to use df.to_csv(), I explicitly need a txt-file. My dataframe is the following:
df
TotalMP ZoneA ZoneB ... Zone11 Zone12 Date_and_time
2 36.0 0.0 0.0 ... 0.0 0.0 2021-01-10 18:00:01.200
3 0.0 0.0 0.0 ... 0.0 0.0 2021-01-10 18:00:01.400
4 0.0 0.0 0.0 ... 0.0 0.0 2021-01-10 18:00:01.600
5 0.0 0.0 0.0 ... 0.0 0.0 2021-01-10 18:00:01.800
6 47.0 0.0 0.0 ... 0.0 0.0 2021-01-10 18:00:02.000
... ... ... ... ... ... ...
143993 40.0 0.0 0.0 ... 0.0 0.0 2021-01-11 01:59:59.400
143994 40.0 0.0 0.0 ... 0.0 0.0 2021-01-11 01:59:59.600
143995 41.0 0.0 0.0 ... 0.0 0.0 2021-01-11 01:59:59.800
143996 54.0 0.0 0.0 ... 0.0 0.0 2021-01-11 02:00:00.000
143997 97.0 0.0 0.0 ... 0.0 0.0 2021-01-11 02:00:00.200
[143981 rows x 18 columns]
I'm trying the following:
np.savetxt('hourly_median_cage1.txt', df, fmt='%f', delimiter=";")
But I think it doesn't recognize the Date_and_time datetime format. How can I fix this? I'd also would index the dataframe on Date_and_time before saving it to a textfile.
Thank you
Upvotes: 1
Views: 64
Reputation: 114
The following should work:
np.savetxt(r'c:\data\np.txt', df, fmt='%f',delimiter=";")
Alternatively you can save it to a .csv and from there convert it into a .txt (although this is not the cleanest solution for sure)
Upvotes: 1
Reputation: 1878
Try this snippet:
df = df.set_index('Date_and_time')
df.to_csv('file.csv')
Upvotes: 1