从元组到熊猫的多列

时间:2021-11-19 22:55:10

How do I convert this dataframe

如何转换这个dataframe

                                          location  value                       
0                   (Richmond, Virginia, nan, USA)    100                       
1              (New York City, New York, nan, USA)    200                       

to this:

:

    city            state       region    country   value
0   Richmond        Virginia    nan       USA       100
1   New York City   New York    nan       USA       200

Note that the location column in the first dataframe contains tuples. I want to create four columns out of the location column.

请注意,第一个dataframe中的location列包含元组。我想从location列中创建4列。

2 个解决方案

#1


8  

new_col_list = ['city','state','regions','country']
for n,col in enumerate(new_col_list):
    df[col] = df['location'].apply(lambda location: location[n])

df = df.drop('location',axis=1)

#2


5  

If you return a Series of the (split) location, you can merge (join to merge on index) the resulting DF directly with your value column.

如果返回一系列(拆分)位置,则可以直接将结果DF与值列合并(在索引上合并到合并)。

addr = ['city', 'state', 'region', 'country']
df[['value']].join(df.location.apply(lambda loc: Series(loc, index=addr)))

   value           city     state  region country
0    100       Richmond  Virginia     NaN     USA
1    200  New York City  New York     NaN     USA

#1


8  

new_col_list = ['city','state','regions','country']
for n,col in enumerate(new_col_list):
    df[col] = df['location'].apply(lambda location: location[n])

df = df.drop('location',axis=1)

#2


5  

If you return a Series of the (split) location, you can merge (join to merge on index) the resulting DF directly with your value column.

如果返回一系列(拆分)位置,则可以直接将结果DF与值列合并(在索引上合并到合并)。

addr = ['city', 'state', 'region', 'country']
df[['value']].join(df.location.apply(lambda loc: Series(loc, index=addr)))

   value           city     state  region country
0    100       Richmond  Virginia     NaN     USA
1    200  New York City  New York     NaN     USA