我有一个 CSV,我将其读入数据框以删除某些列并进行一些操作。
一些示例行是:
20 2/5/1954 13:55 0.5 18
21 2/5/1954 14:35 0.5 18.2
22 2/5/1954 16:35 0.5 18.5
我想删除日期时间中的时间,例如,我得到的2/5/1954不是2/5/1954 13:55.
我写了这个脚本:
import pandas as pd
from datetime import datetime as dt
df = pd.read_csv('habsos_20200310.csv', sep=',', error_bad_lines=False, index_col=False, dtype='unicode')
pd.set_option('display.max_rows', None)
# Get only the columns we care about
dfSub = df[['sample_date','sample_depth','water_temp']]
# Remove the NaN values
dfClean = dfSub.dropna()
# Select 0.5 depth measurements only
dfClean2 = dfClean.loc[df['sample_depth'] == '0.5']
print(dfClean2)
这给了我:
sample_date sample_depth water_temp
20 2/5/1954 13:55 0.5 18
21 2/5/1954 14:35 0.5 18.2
22 2/5/1954 16:35 0.5 18.5
23 2/5/1954 16:52 0.5 18.5
24 2/5/1954 17:10 0.5 18.6
25 2/5/1954 17:25 0.5 18.8
26 2/5/1954 17:43 0.5 19
我试图将这些行添加到我的脚本中以转换该sample_date列:
new_df = dfClean2['sample_date'].str.split()[0]
print(new_df)
Helenr
相关分类