I have a dataset in a following stucture. Timestamp column in this case represents the corresponding weekday. I'd like to re-insert the timestamp as a new column so that I can use it as one of the X-axis values in linear regression. When I try to insert it with:
df["DATE"] = df['TIMESTAMP'].astype(str)
I just receive the following error message: raise KeyError(key) from err
KeyError: 'TIMESTAMP'
Works fine when I tried with any other columns, just not timestamp, how should I proceed?
| Timestamp | Count | Etc. | Etc. |
|---|---|---|---|
| 0 | 23 | 32 | 42 |
| 1 | 84 | 32 | 42 |
| 2 | 12 | 32 | 42 |
| 3 | 64 | 32 | 42 |
| 4 | 15 | 32 | 42 |
| 5 | 32 | 32 | 42 |
| 6 | 53 | 32 | 42 |
I had to reformat the data to get total ordercounts and such by date, as requested here's my code so far:
df['TIMESTAMP']= pd.to_datetime(df['TIMESTAMP'])
df = df[["TIMESTAMP","TEMPERATURE","WIND_SPEED"]]
datecount = df.resample('D', on='TIMESTAMP')['WIND_SPEED'].count()
df["ORDCOUNT"] = datecount.groupby(datecount.index.weekday).mean()
df_mod = df.groupby(df['TIMESTAMP'].dt.weekday).mean()
df_mod["ORDCOUNT"] = datecount.groupby(datecount.index.weekday).mean()
df_mod["DATE"] = df_mod['TIMESTAMP'].astype(str)
CodePudding user response:
TIMESTAMP is now in your index since the groupby define it as index of your output dataframe. You just have to reset_index:
df_mod = df_mod.reset_index()
# OR (if you want to keep a copy)
df_mod['TIMESTAMP'] = df_mod.index
