Python 如何替换 Pandas.DataFrame 上的整列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36846060/
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-08-19 18:26:14  来源:igfitidea点击:

how to replace an entire column on Pandas.DataFrame

pythonpandas

提问by Stefano Fedele

I would like to replace an entire column on a Pandas DataFrame with another column taken from another DataFrame, an example will clarify what I am looking for

我想用从另一个 DataFrame 中获取的另一列替换 Pandas DataFrame 上的整个列,一个示例将阐明我正在寻找的内容

import pandas as pd
dic = {'A': [1, 4, 1, 4], 'B': [9, 2, 5, 3], 'C': [0, 0, 5, 3]}
df = pd.DataFrame(dic)

df is

df 是

'A' 'B' 'C'
 1   9   0
 4   2   0
 1   5   5
 4   3   3

Now I have another dataframe called df1 with a column "E" that is

现在我有另一个名为 df1 的数据框,其中有一列“E”

df1['E'] = [ 4, 4, 4, 0]

and I would like to replace column "B" of df with column "E" of df1

我想用df1的“E”列替换df的“B”列

'A' 'E' 'C'
 1   4   0
 4   4   0
 1   4   5
 4   0   3

I tried to use the .replace() method in many ways but I didn't get anything good. Can you help me?

我尝试以多种方式使用 .replace() 方法,但没有得到任何好处。你能帮助我吗?

采纳答案by Jamie Edgecombe

If you don't mind getting a new data frame object returned as opposed to updating the original Pandas .assign()will avoid SettingWithCopyWarning. Your example:

如果您不介意返回一个新的数据框对象而不是更新原始Pandas,那么 .assign()将避免SettingWithCopyWarning. 你的例子:

df = df.assign(B=df1['E'])

回答by EdChum

If the indices match then:

如果索引匹配,则:

df['B'] = df1['E']

should work otherwise:

否则应该工作:

df['B'] = df1['E'].values

will work so long as the length of the elements matches

只要元素的长度匹配就可以工作

回答by Chege

For those that struggle with the "SettingWithCopy" warning, here's a workaround which may not be so efficient, but still gets the job done.

对于那些因“SettingWithCopy”警告而苦恼的人,这里有一个可能效率不高但仍能完成工作的解决方法。

Suppose you with to overwrite column_1 and column_3, but retain column_2 and column_4

假设您覆盖 column_1 和 column_3,但保留 column_2 和 column_4

columns_to_overwrite = ["column_1", "column_3"]

First delete the columns that you intend to replace...

首先删除要替换的列...

original_df.drop(labels=columns_to_overwrite, axis="columns", inplace=True)

... then re-insert the columns, but using the values that you intended to overwrite

...然后重新插入列,但使用您打算覆盖的值

original_df[columns_to_overwrite] = other_data_frame[columns_to_overwrite]