Remove unwanted content in a column (not the whole column) in Pandas

What if I want to 2016-06-29

be displayed in the timestamp column and remove the unnecessary part like15:46:43.895000

enter image description here

+3


source to share


3 answers


If the timestamps are:

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

      

If the lines are:

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

      



Example:

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

      

+1


source


we can use the method .dt.normalize()

:



df['timestamp'] = df['timestamp'].dt.normalize()

      

+1


source


df['timestamp']=pd.to_datetime(df['timestamp']).dt.strftime('%Y-%m-%d')

      

0


source







All Articles