将 Pandas 系列作为列附加到 DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41509936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:44:08  来源:igfitidea点击:

Append Pandas Series to DataFrame as a column

pandasseries

提问by Amir

I have panadas dataframe (df) like ['key','col1','col2','col3'] and I have pandas series (sr) for which the index is the same as 'key' in data frame. I want to append the series to the dataframe at the new column called col4 with the same 'key'. I have the following code:

我有像 ['key','col1','col2','col3'] 这样的 panadas 数据框(df),并且我有 Pandas 系列(sr),其索引与数据框中的 'key' 相同。我想将该系列附加到名为 col4 的新列的数据框中,并使用相同的“键”。我有以下代码:

for index, row in segmention.iterrows():    
     df[df['key']==row['key']]['col4']=sr.loc[row['key']]

The code is very slow. I assume there should be more efficient and better way to do that. could you please help?

代码很慢。我认为应该有更有效和更好的方法来做到这一点。能否请你帮忙?

采纳答案by jezrael

Use mapas mentioned EdChum:

map如上所述使用EdChum

df['col4'] = df['key'].map(sr)
print (df)
   col1  col2  col3 key  col4
0     4     7     1   A     2
1     5     8     3   B     4
2     6     9     5   C     1

Or assign with set_index:

或分配set_index

df = df.set_index('key')
df['col4'] = sr
print (df)
     col1  col2  col3  col4
key                        
A       4     7     1     2
B       5     8     3     4
C       6     9     5     1

If dont need aligndata in Seriesby keyuse (see difference 2,1,4vs 4,1,2):

如果不需要align在数据Serieskey使用(见区别2,1,4VS 4,1,2):

df['col4'] = sr.values
print (df)
   col1  col2  col3 key  col4
0     4     7     1   A     4
1     5     8     3   B     1
2     6     9     5   C     2

Sample:

样本:

df = pd.DataFrame({'key':[1,2,3],
                   'col1':[4,5,6],
                   'col2':[7,8,9],
                   'col3':[1,3,5]}, index=list('ABC'))

print (df)
   col1  col2  col3  key
A     4     7     1    1
B     5     8     3    2
C     6     9     5    3

sr = pd.Series([4,1,2], index=list('BCA'))
print (sr)
B    4
C    1
A    2
dtype: int64

df['col4'] = df['key'].map(sr)
print (df)
   col1  col2  col3 key  col4
0     4     7     1   A     2
1     5     8     3   B     4
2     6     9     5   C     1


df = df.set_index('key')
df['col4'] = sr
print (df)
     col1  col2  col3  col4
key                        
A       4     7     1     2
B       5     8     3     4
C       6     9     5     1

回答by Jan Zeiseweis

You can simply do:

你可以简单地做:

df['col4'] = sr 

If don't misunderstand.

如果没有误会。

回答by Ted Petrou

This is really a good use case for join, where the left dataframe aligns a column with the index of the right dataframe/series. You have to make sure your Series has a name for it to work

这对于 来说确实是一个很好的用例join,其中左侧数据框将一列与右侧数据框/系列的索引对齐。你必须确保你的系列有一个名字才能工作

sr.name = 'some name'
df.join(sr, on='key')