Pandas:如何从超集的子集向超集添加列?

时间:2016-12-13 02:03:13

标签: python pandas

在这些代码中,In [15]: df4['t']=2并不适用于df3。这不是我想要的。我想要添加列操作也应用于df3,而不是仅应用于df4。(但df4['t']=2没有添加名为t的列但添加一行,困惑我)

另外,我注意到它暗示A value is trying to be set on a copy of a slice from a DataFrame

有什么想法解决这个问题?

In [6]: df2 =pandas. DataFrame(np.random.randn(10, 5))

In [7]: df2
Out[7]:
          0         1         2         3         4
0  0.222512 -0.907183  0.516238 -1.307885  1.604694
1 -0.648315  0.024165  0.487837 -0.374203 -0.193131
2  0.961563  1.847492 -1.773695 -0.791906 -0.458998
3  0.550847  2.221003  0.197836 -1.260352  0.794854
4 -0.211655  0.555512  0.832657 -0.703831 -0.586403
5 -0.384389  1.622995 -0.858065 -0.455278 -1.354076
6 -0.331782  1.256876 -1.080412  1.425681  0.017413
7 -1.008093  0.914414  2.023874 -0.004319  0.733349
8 -0.038734 -0.771304 -0.644371 -0.492886  2.111187
9 -2.812306 -1.434702 -0.074720  1.413066 -0.160265

In [8]: df3=df2

In [9]: df3
Out[9]:
          0         1         2         3         4
0  0.222512 -0.907183  0.516238 -1.307885  1.604694
1 -0.648315  0.024165  0.487837 -0.374203 -0.193131
2  0.961563  1.847492 -1.773695 -0.791906 -0.458998
3  0.550847  2.221003  0.197836 -1.260352  0.794854
4 -0.211655  0.555512  0.832657 -0.703831 -0.586403
5 -0.384389  1.622995 -0.858065 -0.455278 -1.354076
6 -0.331782  1.256876 -1.080412  1.425681  0.017413
7 -1.008093  0.914414  2.023874 -0.004319  0.733349
8 -0.038734 -0.771304 -0.644371 -0.492886  2.111187
9 -2.812306 -1.434702 -0.074720  1.413066 -0.160265

In [10]: df3['d']=1

In [11]: df3
Out[11]:
          0         1         2         3         4  d
0  0.222512 -0.907183  0.516238 -1.307885  1.604694  1
1 -0.648315  0.024165  0.487837 -0.374203 -0.193131  1
2  0.961563  1.847492 -1.773695 -0.791906 -0.458998  1
3  0.550847  2.221003  0.197836 -1.260352  0.794854  1
4 -0.211655  0.555512  0.832657 -0.703831 -0.586403  1
5 -0.384389  1.622995 -0.858065 -0.455278 -1.354076  1
6 -0.331782  1.256876 -1.080412  1.425681  0.017413  1
7 -1.008093  0.914414  2.023874 -0.004319  0.733349  1
8 -0.038734 -0.771304 -0.644371 -0.492886  2.111187  1
9 -2.812306 -1.434702 -0.074720  1.413066 -0.160265  1

In [12]: df2
Out[12]:
          0         1         2         3         4  d
0  0.222512 -0.907183  0.516238 -1.307885  1.604694  1
1 -0.648315  0.024165  0.487837 -0.374203 -0.193131  1
2  0.961563  1.847492 -1.773695 -0.791906 -0.458998  1
3  0.550847  2.221003  0.197836 -1.260352  0.794854  1
4 -0.211655  0.555512  0.832657 -0.703831 -0.586403  1
5 -0.384389  1.622995 -0.858065 -0.455278 -1.354076  1
6 -0.331782  1.256876 -1.080412  1.425681  0.017413  1
7 -1.008093  0.914414  2.023874 -0.004319  0.733349  1
8 -0.038734 -0.771304 -0.644371 -0.492886  2.111187  1
9 -2.812306 -1.434702 -0.074720  1.413066 -0.160265  1

In [13]: df4=df3.loc[:,'d']

In [14]: df4
Out[14]:
0    1
1    1
2    1
3    1
4    1
5    1
6    1
7    1
8    1
9    1
Name: d, dtype: int64

In [15]: df4['t']=2
C:\Users\jiahao\AppData\Local\Programs\Python\Python35\Scripts\ipython:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

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

In [16]: df4
Out[16]:
0    1
1    1
2    1
3    1
4    1
5    1
6    1
7    1
8    1
9    1
t    2
Name: d, dtype: int64

In [17]: df3
Out[17]:
          0         1         2         3         4  d
0  0.222512 -0.907183  0.516238 -1.307885  1.604694  1
1 -0.648315  0.024165  0.487837 -0.374203 -0.193131  1
2  0.961563  1.847492 -1.773695 -0.791906 -0.458998  1
3  0.550847  2.221003  0.197836 -1.260352  0.794854  1
4 -0.211655  0.555512  0.832657 -0.703831 -0.586403  1
5 -0.384389  1.622995 -0.858065 -0.455278 -1.354076  1
6 -0.331782  1.256876 -1.080412  1.425681  0.017413  1
7 -1.008093  0.914414  2.023874 -0.004319  0.733349  1
8 -0.038734 -0.771304 -0.644371 -0.492886  2.111187  1
9 -2.812306 -1.434702 -0.074720  1.413066 -0.160265  1

In [18]:

1 个答案:

答案 0 :(得分:2)

这里有一些误解。声明df4=df3.loc[:,'d']会返回系列,而不是 DataFrame 。所以df4现在是一个系列。系列没有列。它们具有索引引用的值。括号运算符尝试查找Series的索引。您的以下语句df4['t'] = 2会将索引t添加到系列中,并为其指定值2.

通过使用发送到.iloc的列名列表,可以让df4保留DataFrame,如下所示:df4=df3.loc[:,['d']]。 df4现在是一个DataFrame,运行命令df4['t'] = 2现在会向df4追加一列。

您正在收到setwithcopy警告,显示语句df4=df3.loc[:,'d']可能无法创建列d的新副本,因此df4仍可能引用它。但是,df4=df3.loc[:,['d']]似乎是完全独立的DataFrame,并且向其添加列不会创建警告,也不会修改d3,这将需要使用额外的代码行完成。

相关问题