简体   繁体   中英

Pandas: Add Series to DataFrame ordered by column

I am sure this was asked before, but I couldn't find it. I want to add a Series as a new column to the DataFrame. All the Series Index names are contained in one column of the DataFrame, but the Dataframe has more rows than the Series.

DataFrame:
0 London 231
1 Beijing 328
12 New York 920
3 Singapore 1003

Series:
London AB
New York AC
Singapore B

and the result should look like

0 London 231 AB
1 Beijing 328 NaN
12 New York 920 AC
3 Singapore 1003 B

How can I do this without loops? Thanks!

  1. set index to city names for both df and series
  2. combine via pandas merge

import pandas as pd

cities = ['London', 'Beijing', 'New York', 'Singapore']

df_data = {
    'col_1': [0,1,12,3],
    'col_2': [231, 328, 920, 1003],
}

df = pd.DataFrame(df_data, index=cities)

cities2 = ['London','New York','Singapore']

series = pd.Series(['AB', 'AC', 'B'], index=cities2)


combined = pd.merge(
    left=df,
    right=pd.DataFrame(series),
    how='left',
    left_index=True,
    right_index=True
)

print combined

OUTPUT:

           col_1  col_2    0
London         0    231   AB
Beijing        1    328  NaN
New York      12    920   AC
Singapore      3   1003    B

You can use pandas.DataFrame.merge()

df = pd.DataFrame({'A': [0,1,12,3], 'B': ['London', 'Beijing', 'New York', 'Singapore'], 'C': [231, 328, 920, 1003] })
    A          B     C
0   0     London   231
1   1    Beijing   328
2  12   New York   920
3   3  Singapore  1003

s = pd.Series(['AB', 'AC', 'B'], index=['London', 'New York', 'Singapore'])
London       AB
New York     AC
Singapore     B
dtype: object

df2 = pd.DataFrame({'D': s.index, 'E': s.values })
           D   E
0     London  AB
1   New York  AC
2  Singapore   B

Then, you can merge the two data frames:

merged = df.merge(df2, how='left', left_on='B', right_on='D')
    A          B     C          D    E
0   0     London   231     London   AB
1   1    Beijing   328        NaN  NaN
2  12   New York   920   New York   AC
3   3  Singapore  1003  Singapore    B

You can drop columns D

merged = merged.drop('D', axis=1)
    A          B     C    E
0   0     London   231   AB
1   1    Beijing   328  NaN
2  12   New York   920   AC
3   3  Singapore  1003    B

based on @Joe R solution with some modificaiton. say, df is your DataFrame and s is your Series

s = s.to_frame().reset_index()
df = df.merge(s,how='left',left_on=df['B'],right_on=s['index']).ix[:,[0,1,3]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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