Python 将列中的所有值复制到 Pandas 数据框中的新列

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

Copy all values in a column to a new column in a pandas dataframe

pythonpandas

提问by Justin Buchanan

This is a very basic question, I just can not seem to find an answer.

这是一个非常基本的问题,我似乎无法找到答案。

I have a dataframe like this, called df:

我有一个这样的数据框,叫做 df:

  A     B     C
 a.1   b.1   c.1
 a.2   b.2   c.2
 a.3   b.3   c.3

Then I extract all the rows from df, where column 'B' has a value of 'b.2'. I assign these results to df_2.

然后我从 df 中提取所有行,其中列 'B' 的值为 'b.2'。我将这些结果分配给 df_2。

df_2 = df[df['B'] == 'b.2']

df_2 becomes:

df_2 变为:

  A     B     C
 a.2   b.2   c.2

Then, I copy all the values in column 'B' to a new column named 'D'. Causing df_2 to become:

然后,我将“B”列中的所有值复制到名为“D”的新列中。导致 df_2 变为:

  A     B     C     D
 a.2   b.2   c.2   b.2

When I preform an assignment like this:

当我执行这样的作业时:

df_2['D'] = df_2['B']

I get the following warning:

我收到以下警告:

A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

试图在来自 DataFrame 的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value 代替

请参阅文档中的警告:http: //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy



I have also tried using .loc when creating df_2 like this:

在创建 df_2 时,我也尝试使用 .loc ,如下所示:

df_2 = df.loc[df['B'] == 'b.2']

However, I still get the warning.

但是,我仍然收到警告。

Any help is greatly appreciated.

任何帮助是极大的赞赏。

采纳答案by Anand S Kumar

You can simply assign the Bto the new column , Like -

您可以简单地将 分配B给新列,例如 -

df['D'] = df['B']


Example/Demo -

示例/演示 -

In [1]: import pandas as pd

In [2]: df = pd.DataFrame([['a.1','b.1','c.1'],['a.2','b.2','c.2'],['a.3','b.3','c.3']],columns=['A','B','C'])

In [3]: df
Out[3]:
     A    B    C
0  a.1  b.1  c.1
1  a.2  b.2  c.2
2  a.3  b.3  c.3

In [4]: df['D'] = df['B']                  #<---What you want.

In [5]: df
Out[5]:
     A    B    C    D
0  a.1  b.1  c.1  b.1
1  a.2  b.2  c.2  b.2
2  a.3  b.3  c.3  b.3

In [6]: df.loc[0,'D'] = 'd.1'

In [7]: df
Out[7]:
     A    B    C    D
0  a.1  b.1  c.1  d.1
1  a.2  b.2  c.2  b.2
2  a.3  b.3  c.3  b.3

回答by Alex

The problem is in the line before the one that throws the warning. When you create df_2 that's where you're creating a copy of a slice of a dataframe. Instead, when you create df_2, use .copy() and you won't get that warning later on.

问题出在发出警告的那一行之前。当您创建 df_2 时,您正在创建数据帧切片的副本。相反,当您创建 df_2 时,使用 .copy() 并且您以后不会收到该警告。

df_2 = df[df['B'] == 'b.2'].copy()

回答by Luke

I think the correct access method is using the index:

我认为正确的访问方法是使用索引:

df_2.loc[:,'D'] = df_2['B']

回答by Mark Andersen

Following up on these solutions, here is some helpful code illustrating :

跟进这些解决方案,这里有一些有用的代码说明:

#
# Copying columns in pandas without slice warning
#
import numpy as np
df = pd.DataFrame(np.random.randn(10, 3), columns=list('ABC'))

#
# copies column B into new column D
df.loc[:,'D'] = df['B']
print df

#
# creates new column 'E' with values -99
# 
# But copy command replaces those where 'B'>0 while others become NaN (not copied)
df['E'] = -99
print df
df['E'] = df[df['B']>0]['B'].copy()
print df

#
# creates new column 'F' with values -99
# 
# Copy command only overwrites values which meet criteria 'B'>0
df['F']=-99
df.loc[df['B']>0,'F'] = df[df['B']>0]['B'].copy()
print df

回答by KarthikS

How about:

怎么样:

df['D'] = df['B'].values

df['D'] = df['B'].values