布尔索引可以产生一个大 pandas 数据框的视图? [英] boolean indexing that can produce a view to a large pandas dataframe?
问题描述
视图
到原来。问题是,花哨的索引总是返回一个复制
。思想的 .ix
方法,但使用 df.ix []
方法的布尔索引也返回一个副本。 基本上如果 df
是我的数据框,我想查看列C,使$ C!= 0,A == 10,B< 30,...
等在熊猫中有快速的方法吗?
即使 df.loc [idx]
可能是一个副本的一部分 df
, 赋值到 df.loc [idx]
修改 df
本身。 (这也适用于 df.iloc
和 df.ix
。)
例如,
将pandas导入pd
导入numpy作为np
df = pd.DataFrame({'A':[9,10] * 6,
'B':range(23,35),
'C':range(-6,6)})
打印(df)
#ABC
#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
这是我们的布尔索引:
idx =(df ['C' != 0)& (df ['A'] == 10)& (df ['B']< 30)
我们可以修改 df
其中 idx
通过分配给 df.loc [idx,...]
。例如,
df.loc [idx,'A'] + = df.loc [idx,'B'] * df.loc [idx,'C']
print(df)
p>
ABC
0 9 23 -6
1 -110 24 -5
2 9 25 - 4
3 -68 26 -3
4 9 27 -2
5 -18 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
Got a large dataframe that I want to take slices of (according to multiple boolean criteria), and then modify the entries in those slices in order to change the original dataframe -- i.e. I need a view
to the original. Problem is, fancy indexing always returns a copy
. Thought of the .ix
method, but boolean indexing with the df.ix[]
method also returns a copy.
Essentially if df
is my dataframe, I'd like a view to column C such that C!=0, A==10, B<30,...
etc. Is there a fast way to do this in pandas?
Even though df.loc[idx]
may be a copy of a portion of df
, assignment to df.loc[idx]
modifies df
itself. (This is also true of df.iloc
and df.ix
.)
For example,
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
Here is our boolean index:
idx = (df['C']!=0) & (df['A']==10) & (df['B']<30)
We can modify those rows of df
where idx
is True by assigning to df.loc[idx, ...]
. For example,
df.loc[idx, 'A'] += df.loc[idx, 'B'] * df.loc[idx, 'C']
print(df)
yields
A B C
0 9 23 -6
1 -110 24 -5
2 9 25 -4
3 -68 26 -3
4 9 27 -2
5 -18 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
这篇关于布尔索引可以产生一个大 pandas 数据框的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!