[英]Pandas - CSV file , how to take Month date from list of int32 column
Hey Guys im trying to create a new col, from a dates col which like: "1/20/21, 5/30/21.. 11/5/21", so i have 2 options here: 1 char, or 2 char till get "/" char.嘿,伙计们,我正在尝试从日期 col 创建一个新的 col,例如:“1/20/21、5/30/21.. 11/5/21”,所以我在这里有 2 个选项:1 个字符或 2 个char 直到得到“/”字符。
all_data['Month'] = all_data['Order Date'].str[0:1]
all_data['Month'] = all_data['Month'].astype('int32')
all_data.head()
This option is takes only 1-9 months.. without 10,11,12.这个选项只需要 1-9 个月.. 没有 10,11,12。 i tried to make:
我试着做:
index = all_data['Order Date'].find('\') # return the index of "/"
all_data['Month'] = all_data['Order Date'].str[0:index]
But its also not working... got error of type value.但它也不起作用......得到类型值的错误。 how can i make it works?
我怎样才能让它工作?
I think here is simplier convert values to datetimes and then use Series.dt.month
:我认为这里更简单地将值转换为日期时间,然后使用
Series.dt.month
:
all_data['Month'] = pd.to_datetime(all_data['Order Date']).dt.month
Or split by /
and get first value:或除以
/
并获得第一个值:
all_data['Month'] = all_data['Order Date'].str.split('/').str[0].astype(int)
Or get numeric before start of string and first /
in Series.str.extract
:或者在字符串开头和第一个
/
在Series.str.extract
之前获取数字:
all_data['Month'] = all_data['Order Date'].str.extract('^(\d+)/').astype(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.