简体   繁体   English

使用pandas python将字符串日期转换为列中所有值的数字

[英]convert string date to numeric for all values in column using pandas python

I need to transform "Jan 11, 2017 9:00 PM" to 2017-01-11 21:00:00 for all values in ColumnA. 对于ColumnA中的所有值,我需要将“2017年1月11日9:00 PM”转换为2017-01-11 21:00:00。

Is there an easy way / function for doing this in python using pandas? 使用pandas在python中有一个简单的方法/功能吗?

to_datetime seems to handle this just fine: to_datetime似乎处理这个很好:

In [6]:
pd.to_datetime('Jan 11, 2017 9:00 PM')

Out[6]:
Timestamp('2017-01-11 21:00:00')

So you can just overwrite your column using the output from this function: 所以你可以使用这个函数的输出覆盖你的列:

df['ColumnA'] = pd.to_datetime(df['ColumnA'])

And just to clarify that the dates and months are correct here: 只是为了澄清这里的日期和月份是正确的:

In [8]:
pd.to_datetime('Jan 11, 2017 9:00 PM').day

Out[8]:
11

In [9]:
pd.to_datetime('Jan 11, 2017 9:00 PM').month

Out[9]:
1

暂无
暂无

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

相关问题 如何将所有时间戳值转换为数据帧列Python Pandas中的数值 - How to convert all timestamp values to a numeric value in a dataframe column Python Pandas Python:如何从pandas列中删除所有非数值? - Python: how to drop all the non numeric values from a pandas column? Python Pandas:使用公共列将3个具有字符串,数字和NaN值的数据框分组为一个新的数据框 - Python Pandas: groupby 3 dataframes with string, numeric and NaN values to a new dataframe using a common column Pandas - 使用替换+正则表达式从字符串列中提取数值 - Pandas - extract numeric values from string column using replace + regex 在python中,如何使用pandas将字符串日期列转换为日期格式? - In python, how do I convert my string date column to date format using pandas? 在 python pandas 中将天差转换为数值 - convert days difference to numeric values in python pandas 使用 python pandas 将行值转换为列以将日期字段分配给每个新列 - Convert row values to columns to assign date field to each new column using python pandas 如何使用 Python Pandas 将 csv 中的列转换为年、月、周的字符串值? - How can i convert column into days in csv having string values in years , months , weeks using Python Pandas? Python:熊猫数据框将字符串和值转换为列类别 - Python: pandas data frame convert string and values into column category 如何将列中的所有值从数千转换为数十亿? 使用 Pandas - How to convert all the values in a column, from thousands to billions? Using Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM