簡體   English   中英

熊貓:如何從超集的子集向超集添加列?

[英]Pandas: How to add columns to a superset from a subset of the superset?

在這些代碼中, 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]:

這里有一些誤會。 語句df4=df3.loc[:,'d']返回Series而不是DataFrame 因此df4現在是系列。 系列沒有欄。 它們具有由索引引用的值。 方括號運算符試圖找到您系列的索引。 您的以下語句df4['t'] = 2將索引t添加到Series並為其分配值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,而這必須使用另一行代碼來完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM