[英]How to get the date format of different Columns in pandas
In my pandas dataframe I have 2 to 3 different DateTime columns.在我的 pandas dataframe 我有 2 到 3 个不同的 DateTime 列。 Now I want to show the format of that date columns
现在我想显示该日期列的格式
Example:例子:
Column1![]() |
Column2![]() |
---|---|
2016-03-12 23:24:05 ![]() |
26-03-2016 11:23:09 AM ![]() |
Now it should print the format: YYYY-MM-DD HH:MM:SS for Column1 and DD-MM-YYYY HH:MM:SS AM for Column2现在它应该打印格式: YYYY-MM-DD HH:MM:SS for Column1 和 DD-MM-YYYY HH:MM:SS AM for Column2
I am not aware of anything that parses the datetime formats, also strftime and strptime dosnot have the formats you show.我不知道任何解析日期时间格式的东西,strftime 和 strptime 也没有你显示的格式。 You would have to rely on the default formats and using them you have to create all possible formats and your expected value in a dictionary.
您将不得不依赖默认格式并使用它们,您必须在字典中创建所有可能的格式和您的预期值。 Then you can do below:
然后你可以在下面做:
formats={'%Y-%m-%d %H:%M:%S':'YYYY-MM-DD HH:MM:SS',
'%d-%m-%Y %H:%M:%S AM':'DD-MM-YYYY HH:MM:SS AM'}
possibilities = pd.MultiIndex.from_product((df.columns,formats.keys()))
u = df.astype(str) #if the datetime columns are not strings already
d = {}
for i in possibilities:
try:
pd.to_datetime(u[i[0]],format=i[1])
d[i[0]] = formats.get(i[1])
except ValueError:
pass
print(d)
{'Column1': 'YYYY-MM-DD HH:MM:SS', 'Column2': 'DD-MM-YYYY HH:MM:SS AM'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.