gpt4 book ai didi

pandas - 删除 Pandas 中列(不是整列)中不需要的内容

转载 作者:行者123 更新时间:2023-12-02 01:09:51 25 4
gpt4 key购买 nike

如果我只想在时间戳列中显示2016-06-29,并删除不需要的部分,如15:46:43.895000,该怎么办?

enter image description here

最佳答案

如果时间戳:

 df['timestamp'] = df['timestamp'].dt.floor('d')

如果字符串:

 df['timestamp'] = df['timestamp'].str.split().str[0]

示例:

df = pd.DataFrame({'timestamp':pd.date_range('2016-06-29 15:46:43.895000', 
periods=3,
freq='2000T')})
print (df)
timestamp
0 2016-06-29 15:46:43.895
1 2016-07-01 01:06:43.895
2 2016-07-02 10:26:43.895

print (type(df.loc[0, 'timestamp']))
<class 'pandas._libs.tslib.Timestamp'>

df['timestamp'] = df['timestamp'].dt.floor('d')
print (df)
timestamp
0 2016-06-29
1 2016-07-01
2 2016-07-02

df = pd.DataFrame({'timestamp':['2016-06-20 15:46:43.895000', 
'2016-06-22 15:46:43.895000',
'2016-06-29 15:46:43.895000']})
print (df)
timestamp
0 2016-06-20 15:46:43.895000
1 2016-06-22 15:46:43.895000
2 2016-06-29 15:46:43.895000

print (type(df.loc[0, 'timestamp']))
<class 'str'>

df['timestamp'] = df['timestamp'].str.split().str[0]
print (df)
timestamp
0 2016-06-20
1 2016-06-22
2 2016-06-29

关于pandas - 删除 Pandas 中列(不是整列)中不需要的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45549965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com