删除Pandas中列(而不是整个列)中不需要的内容
如果我只想在时间戳列中显示删除Pandas中列(而不是整个列)中不需要的内容,pandas,Pandas,如果我只想在时间戳列中显示2016-06-29,并删除不需要的部分,如15:46:43.895000 如果时间戳: 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',
2016-06-29
,并删除不需要的部分,如15:46:43.895000
如果时间戳:
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':pd.date_范围('2016-06-29 15:46:43.895000'),
周期=3,
freq='2000T')})
打印(df)
时间戳
0 2016-06-29 15:46:43.895
1 2016-07-01 01:06:43.895
2 2016-07-02 10:26:43.895
打印(类型(df.loc[0,'时间戳']))
df['timestamp']=df['timestamp'].dt.floor('d')
打印(df)
时间戳
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']})
打印(df)
时间戳
0 2016-06-20 15:46:43.895000
1 2016-06-22 15:46:43.895000
2 2016-06-29 15:46:43.895000
打印(类型(df.loc[0,'时间戳']))
df['timestamp']=df['timestamp'].str.split().str[0]
打印(df)
时间戳
0 2016-06-20
1 2016-06-22
2 2016-06-29
我们可以使用.dt.normalize()
方法:
df['timestamp'] = df['timestamp'].dt.normalize()
你能展示一下你试过的代码吗?如果我(或其他)的答案有帮助,别忘了-点击复选标记(
✓答案旁边的代码>),将其从灰显切换为填充。谢谢
df['timestamp'] = df['timestamp'].dt.normalize()
df['timestamp']=pd.to_datetime(df['timestamp']).dt.strftime('%Y-%m-%d')