小编典典

从 JSON 文件数据更改列名

all

我在我的数据中添加了一个datetime,我想摆脱旧的,所以它看起来更干净!是否可以将标题从更改data.dataset为其他内容?

我试过 df.rename(columns={“data.datasets”:”mydata”}) 但不幸的是没有变化:(

import datetime

#convert the column to datetime:
df["data.date"] = pd.to_datetime(df["data.date"])
df["date"] = df["data.date"].apply(lambda x : x.date())

df.date = pd.to_datetime(df.date)
df.set_index('date', inplace=True)

# First for the day:
df = df.resample('d').first()

# Print only 5/05 - 6/05
print(df.loc[df.index.to_series().between('2022-05-05', '2022-06-05')])

这是当前数据的样子:

   data.date  data.datasets        Time       date                                                     
2022-05-05 2022-05-05 04:19:35         3.0500  2022-05-05
2022-05-06 2022-05-06 00:10:24         2.3500  2022-05-06
2022-05-07 2022-05-07 00:10:47         4.0900  2022-05-07
2022-05-08 2022-05-08 00:08:13         3.8790  2022-05-08
2022-05-09 2022-05-09 00:13:24         3.5780  2022-05-09
2022-05-10 2022-05-10 00:08:49         3.7500  2022-05-10
2022-05-11 2022-05-11 00:09:33         4.3900  2022-05-11
2022-05-12 2022-05-12 00:08:24         3.6900  2022-05-12
2022-05-13 2022-05-13 00:08:45         3.4300  2022-05-13
2022-05-14 2022-05-14 00:08:55         3.9500  2022-05-14

阅读 60

收藏
2022-06-27

共1个答案

小编典典

通过提供字典 {} 重命名列:

df.rename(columns={"data.datasets":"mydata"})

删除列:

df.drop(columns="data.date", inplace=True)
2022-06-27