对数据帧使用逻辑或布尔索引的正确语法是什么?

2024-04-20 10:30:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用逻辑索引来修改Pandas数据帧(版本0.15.2)中的值,如post中所述。我一直收到以下警告:

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
  self.obj[item_labels[indexer[info_axis]]] = value

下面是一个例子来说明。你知道吗

import pandas as pd
import numpy as np
df = pd.DataFrame({'A':[9,10]*6,
                   'B':range(23,35),
                   'C':range(-6,6)})

print df
     A   B  C
0    9  23 -6
1   10  24 -5
2    9  25 -4
3   10  26 -3
4    9  27 -2
5   10  28 -1
6    9  29  0
7   10  30  1
8    9  31  2
9   10  32  3
10   9  33  4
11  10  34  5

使用逻辑索引更改值的正确方法是什么?假设我想从B列中所有大于30的值中减去10,为什么不首选下面的方法?我意识到这是一项被束缚的任务,我对此感到泄气。在我实际使用的代码中,它确实做了我想要做的事情(它不是制作副本,而是实际编辑原始数据帧),但它仍然显示警告:

df['B-type'] = 'B'                  # create column with dummy values
df['B-type'][df['B'] > 30] = 'BI'   # populate the column with real values for BI type
df['B-type'][df['B'] <= 30] = 'BII' # populate the column with real values for BII type
print df
     A   B  C B-type
0    9  23 -6    BII
1   10  24 -5    BII
2    9  25 -4    BII
3   10  26 -3    BII
4    9  27 -2    BII
5   10  28 -1    BII
6    9  29  0    BII
7   10  30  1    BII
8    9  31  2     BI
9   10  32  3     BI
10   9  33  4     BI
11  10  34  5     BI

现在还不清楚为什么这是“错误的”,但仍然工作良好。你知道吗


Tags: the警告dataframepandasdfvaluetypewith
2条回答

这种访问方式称为链式分配,应该避免使用documentation。它不能按预期工作的原因是更新了数据帧的副本,而不是视图。这意味着原始数据帧保持不变。你知道吗

考虑这个链式分配:

df[df['B'] > 30]['B'] = -999

相当于:

df_something = df[df['B'] > 30]
df_something['B'] = -999

>>> print df
     A   B  C
0    9  23 -6
1   10  24 -5
2    9  25 -4
3   10  26 -3
4    9  27 -2
5   10  28 -1
6    9  29  0
7   10  30  1
8    9  31  2
9   10  32  3
10   9  33  4
11  10  34  5

>>> print df_something
     A    B  C
8    9 -999  2
9   10 -999  3
10   9 -999  4
11  10 -999  5

可以看出,确实创建并更新了一个副本,这就是警告的内容。执行此类分配的正确方法是避免链接,即仅通过使用适当索引器的单个操作:

df.loc[df['B'] > 30, 'B'] = -999

注意,这与df.loc[df['B'] > 30]['B'] = -999不同,后者也是链式赋值。你知道吗

一种方法是使用^{}如下-

df.loc[df['B'] > 30,'B'] = df.loc[df['B'] > 30,'B'] - 10

演示-

In [9]: df = pd.DataFrame({'A':[9,10]*6,
   ...:                    'B':range(23,35),
   ...:                    'C':range(-6,6)})

In [10]:

In [10]: df
Out[10]:
     A   B  C
0    9  23 -6
1   10  24 -5
2    9  25 -4
3   10  26 -3
4    9  27 -2
5   10  28 -1
6    9  29  0
7   10  30  1
8    9  31  2
9   10  32  3
10   9  33  4
11  10  34  5

In [11]: df.loc[df['B'] > 30,'B'] = df.loc[df['B'] > 30,'B'] - 10

In [12]: df
Out[12]:
     A   B  C
0    9  23 -6
1   10  24 -5
2    9  25 -4
3   10  26 -3
4    9  27 -2
5   10  28 -1
6    9  29  0
7   10  30  1
8    9  21  2
9   10  22  3
10   9  23  4
11  10  24  5

或者如评论中所述,您也可以使用上述的扩充赋值版本-

df.loc[df['B'] > 30,'B'] -= 10

相关问题 更多 >