简体   繁体   English

PySpark:为所有日期创建 dataframe 的子集

[英]PySpark: Create a subset of a dataframe for all dates

I have a DataFrame that has a lot of columns and I need to create a subset of that DataFrame that has only date values.我有一个 DataFrame 有很多列,我需要创建一个只有日期值的 DataFrame 的子集。

For eg my Dataframe could be:例如,我的 Dataframe 可以是:

1, 'John Smith', '12/10/1982', '123 Main St', '01/01/2000'
2, 'Jane Smith', '11/21/1999', 'Abc St', '12/12/2020'

And my new DataFrame should only have:而我的新 DataFrame 应该只有:

'12/10/1982', '01/01/2000'
'11/21/1999', '12/12/2000'

The dates could be of any format and could be on any column.日期可以是任何格式,并且可以在任何列上。 I can use the dateutil.parser to parse them to make sure they are dates.我可以使用 dateutil.parser 来解析它们以确保它们是日期。 But not sure how to call parse() on all the columns and only filter those that return true to another dataframe, easily.但不确定如何在所有列上调用 parse() 并仅将那些返回 true 的列过滤到另一个 dataframe,轻松。

If you know what you columns the datetimes are in it's easy:如果您知道日期时间所在的列,则很容易:

pd2 = pd[["row_name_1", "row_name_2"]]
# or 
pd2 = pd.iloc[:, [2, 4]]

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

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