从熊猫的数据框中选择日期时间

时间:2020-08-31 21:47:24

标签: python pandas datetime

我正在清理一个凌乱的excel文件,如果存在datetime字段,则试图选择行。这是我的数据框。

           Unnamed: 0          June              2020           Unnamed: 3           Town     Permit          
0           DATE              PERMIT #        OWNER/BUILDER     PERMIT ADDRESS       Center   Code
1      2020-06-02 00:00:00     17785          Joe W             341 Ameth Way        NaN       BF      
2      2020-06-02 00:00:00     17786          Deinise S         198 Cedar Cir        NaN       MR     
3      2020-06-02 00:00:00     17787          John S            49 Jasp Way          NaN       MR 

我想创建一个条件语句,该条件语句将搜索数据框,并且如果某行中存在日期时间,请将该行保留在其他行中。我也想删除标题。所需结果:

1      2020-06-02 00:00:00     17785          Joe W             341 Ameth Way        NaN       BF      
2      2020-06-02 00:00:00     17786          Deinise S         198 Cedar Cir        NaN       MR     
3      2020-06-02 00:00:00     17787          John S            49 Jasp Way          NaN       MR 

2 个答案:

答案 0 :(得分:0)

#Concat  Unnamed: and 0 into one column to make it complete datet_time
df['Unnamed:']=df['Unnamed:'].str.cat(df['0'], sep=' ')

#Coerce df['Unnamed:'] into datetime. That will make any non datetime into NaNs or NaTs
df['Unnamed:']=pd.to_datetime(df['Unnamed:'],errors='coerce')

#Drop any NaN
d`f.dropna(inplace=True)`

答案 1 :(得分:0)

我不喜欢必须基于列名清除数据的想法,所以我将其基于索引。

# convert the first column to a datetime column, put NaT in non-date fields
df['Date'] = pd.to_datetime(df.iloc[:,0], errors='coerce')

# drop any rows that did not convert to a datetime
df = df.dropna(subset=['Date'])