简体   繁体   English

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

[英]Select datetime from dataframe in pandas

I'm cleaning up a messy excel file and am trying to select rows if a datetime field exists.我正在清理一个凌乱的 excel 文件,如果存在日期时间字段,我正在尝试选择行。 This is the dataframe I have.这是我拥有的数据框。

           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 

I'd like to create a conditional statement that will search the dataframe and if a datetime exists in a row, keep the row else drop it.我想创建一个条件语句来搜索数据框,如果一行中存在日期时间,则保留该行,否则将其删除。 I would also like to drop the headers.我也想删除标题。 Desired result:想要的结果:

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 
#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)`

I didn't like the idea of having to base my data clean up off a column name, so instead I based it off an index.我不喜欢必须根据列名清理数据的想法,所以我基于索引。

# 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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM