Pandas Dataframe:将列拆分为多个列,右对齐不一致的单元格条目

时间:2022-02-09 21:39:48

I have a pandas dataframe with a column named 'City, State, Country'. I want to separate this column into three new columns, 'City, 'State' and 'Country'.

我有一个pandas数据框,其中包含一个名为“City,State,Country”的列。我想将这个专栏分为三个新专栏:'City,'State'和'Country'。

0                 HUN
1                 ESP
2                 GBR
3                 ESP
4                 FRA
5             ID, USA
6             GA, USA
7    Hoboken, NJ, USA
8             NJ, USA
9                 AUS

Splitting the column into three columns is trivial enough:

将列拆分为三列非常简单:

location_df = df['City, State, Country'].apply(lambda x: pd.Series(x.split(',')))

However, this creates left-aligned data:

但是,这会创建左对齐数据:

     0       1       2
0    HUN     NaN     NaN
1    ESP     NaN     NaN
2    GBR     NaN     NaN
3    ESP     NaN     NaN
4    FRA     NaN     NaN
5    ID      USA     NaN
6    GA      USA     NaN
7    Hoboken  NJ     USA
8    NJ      USA     NaN
9    AUS     NaN     NaN

How would one go about creating the new columns with the data right-aligned? Would I need to iterate through every row, count the number of commas and handle the contents individually?

如何在数据右对齐的情况下创建新列?我是否需要遍历每一行,计算逗号的数量并单独处理内容?

2 个解决方案

#1


45  

I'd do something like the following:

我会做类似以下的事情:

foo = lambda x: pd.Series([i for i in reversed(x.split(','))])
rev = df['City, State, Country'].apply(foo)
print rev

      0    1        2
0   HUN  NaN      NaN
1   ESP  NaN      NaN
2   GBR  NaN      NaN
3   ESP  NaN      NaN
4   FRA  NaN      NaN
5   USA   ID      NaN
6   USA   GA      NaN
7   USA   NJ  Hoboken
8   USA   NJ      NaN
9   AUS  NaN      NaN

I think that gets you what you want but if you also want to pretty things up and get a City, State, Country column order, you could add the following:

我认为这样可以获得你想要的东西,但是如果你想要了解更多内容并获得City,State,Country列顺序,你可以添加以下内容:

rev.rename(columns={0:'Country',1:'State',2:'City'},inplace=True)
rev = rev[['City','State','Country']]
print rev

     City State Country
0      NaN   NaN     HUN
1      NaN   NaN     ESP
2      NaN   NaN     GBR
3      NaN   NaN     ESP
4      NaN   NaN     FRA
5      NaN    ID     USA
6      NaN    GA     USA
7  Hoboken    NJ     USA
8      NaN    NJ     USA
9      NaN   NaN     AUS

#2


7  

Since you are dealing with strings I would suggest the amendment to your current code i.e.

由于您正在处理字符串,我建议修改您当前的代码,即

location_df = df[['City, State, Country']].apply(lambda x: pd.Series(str(x).split(',')))

I got mine to work by testing one of the columns but give this one a try.

我通过测试其中一个列让我的工作,但试试这个。

#1


45  

I'd do something like the following:

我会做类似以下的事情:

foo = lambda x: pd.Series([i for i in reversed(x.split(','))])
rev = df['City, State, Country'].apply(foo)
print rev

      0    1        2
0   HUN  NaN      NaN
1   ESP  NaN      NaN
2   GBR  NaN      NaN
3   ESP  NaN      NaN
4   FRA  NaN      NaN
5   USA   ID      NaN
6   USA   GA      NaN
7   USA   NJ  Hoboken
8   USA   NJ      NaN
9   AUS  NaN      NaN

I think that gets you what you want but if you also want to pretty things up and get a City, State, Country column order, you could add the following:

我认为这样可以获得你想要的东西,但是如果你想要了解更多内容并获得City,State,Country列顺序,你可以添加以下内容:

rev.rename(columns={0:'Country',1:'State',2:'City'},inplace=True)
rev = rev[['City','State','Country']]
print rev

     City State Country
0      NaN   NaN     HUN
1      NaN   NaN     ESP
2      NaN   NaN     GBR
3      NaN   NaN     ESP
4      NaN   NaN     FRA
5      NaN    ID     USA
6      NaN    GA     USA
7  Hoboken    NJ     USA
8      NaN    NJ     USA
9      NaN   NaN     AUS

#2


7  

Since you are dealing with strings I would suggest the amendment to your current code i.e.

由于您正在处理字符串,我建议修改您当前的代码,即

location_df = df[['City, State, Country']].apply(lambda x: pd.Series(str(x).split(',')))

I got mine to work by testing one of the columns but give this one a try.

我通过测试其中一个列让我的工作,但试试这个。