简体   繁体   English

Pandas:以 Pythonic 的方式分割字符串

[英]Pandas: split string in a pythonic way

I have a pandas Series date that looks like this:我有一个如下所示的熊猫系列date

date       | ...
09.01.2000 |
02.02.2000 |
...

The format is DD-MM-YYYY.格式为 DD-MM-YYYY。 I want to split them into three columns Day, Month and Year.我想将它们分成三列日、月和年。 I tried:我试过:

col = date["date"].str.split(".", expand = True)
date["day"] = col[0]
date["month"] = col[1]
...

It is quite inconvenient so is there a more pythonic way?这很不方便,所以有更pythonic的方式吗? I also tried pd.to_datetime but that is not the short way.我也试过 pd.to_datetime 但这不是捷径。

You can do multiple column assignments in a single line:您可以在一行中进行多个列分配:

df[['day', 'month', 'year']] = df['date'].str.split('.', expand=True)

         date day month  year
0  09.01.2000  09    01  2000
1  02.02.2000  02    02  2000

One option is to use a single assignment:一种选择是使用单个分配:

date['date'], date['month'] = col

This assumes that split() returns a list with exactly two elements.这假设split()返回一个包含两个元素的列表。

You can do something like this.你可以做这样的事情。

import pandas as pd
df = pd.DataFrame({'date':['09.01.2000', '02.02.2000']})

df['mon'],df['day'],df['year'] = zip(*df['date'].str.split('.'))
print (df)

It will give you the below dataframe.它将为您提供以下数据框。 If you don't want df['date'] , then you can use drop() function to drop the column.如果您不想要df['date'] ,则可以使用 drop() 函数删除该列。

         date mon day  year
0  09.01.2000  09  01  2000
1  02.02.2000  02  02  2000

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

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