我有两个具有多个级别和列的多索引数据框。我正在寻找迭代数据框并计数的最快方法,对于每一行,每个数据框中的特定值以上有多少个单元格,然后找到得分至少为一个的两个数据框的行的交点。
现在,我正在使用for循环和groupby的组合来遍历数据帧,但是花了我太多时间才能找到正确的答案(我的实际数据帧包含数千个级别和数百列),所以我需要找到一种不同的方法。
例如:
idx = pd.MultiIndex.from_product([[0,1],[0,1,2]],names=
['index_1','index_2'])
col = ['column_1', 'column_2']
values_list_a=[[1,2],[2,2],[2,1],[-8,1],[2,0],[2,1]]
DFA = pd.DataFrame(values_list_a, idx, col)
DFA:
columns_1 columns2
index_1 index_2
0 0 1 2
1 2 2
2 2 1
1 0 -8 1
1 2 0
2 2 1
values_list_b=[[2,2],[0,1],[2,2],[2,2],[1,0],[1,2]]
DFB = pd.DataFrame(values_list_b, idx, col)
DFB:
columns_1 columns2
index_1 index_2
0 0 2 2
1 0 1
2 2 2
1 0 2 2
1 1 0
2 1 2
我期望的是:
第1步计数发生次数:
DFA:
columns_1 columns2 counts
index_1 index_2
0 0 1 2 1
1 2 2 2
2 2 1 1
1 0 -8 1 0
1 2 0 1
2 2 1 1
DFB:
columns_1 columns2 counts
index_1 index_2
0 0 2 2 2
1 0 1 0
2 2 2 2
1 0 2 2 2
1 1 0 0
2 1 2 1
步骤2:计数大于0的2个数据框的交集应创建一个这样的新数据框(其中,两个数据框中在同一索引中得分至少为1的行被记录,并添加新的index_0级别) 。 index_0 = 0应该引用DFA,index_0 = 1引用DFB:
DFC:
columns_1 columns2 counts
index_0 index_1 index_2
0 0 0 1 2 1
2 2 1 1
1 2 2 1 1
1 0 0 2 2 2
2 2 2 2
1 2 1 2 1
答案 0 :(得分:0)
df.groupby(['index_0','index_1', 'index2'])
现在,您要使用等效的sql,即
df.filter(lambda x: len(x.columns_1) > 2)
df.count()
这是一个概念,我不明白您要过滤什么, 请注意,x是一个组,因此您需要对其进行操作(len,set,value)等
答案 1 :(得分:0)
使用过滤器,.any()和pd.merge()
重新创建数据框:
idx = pd.MultiIndex.from_product([[0,1],[0,1,2]], names=['one', 'two'])
columns = ['columns_1', 'columns_2']
DFA = pd.DataFrame(np.random.randint(-1,3, size=[6,2]), idx, columns)
DFB = pd.DataFrame(np.random.randint(-1,3, size=[6,2]), idx, columns)
print(DFA)
columns_1 columns_2
one two
0 0 -1 2
1 2 -1
2 -1 0
1 0 1 2
1 0 0
2 -1 -1
print(DFB)
columns_1 columns_2
one two
0 0 2 -1
1 1 2
2 2 1
1 0 0 0
1 -1 2
2 1 -1
在这种情况下,过滤数据框以获取大于1的值。
DFA = DFA.loc[(DFA>1).any(bool_only=True, axis=1),:]
DFB = DFB.loc[(DFB>1).any(bool_only=True, axis=1),:]
print(DFA)
columns_1 columns_2
one two
0 0 -1 2
1 2 -1
1 0 1 2
print(DFB)
columns_1 columns_2
one two
0 0 2 -1
1 1 2
2 2 1
1 1 -1 2
将两者合并。使用out join使您接近。不确定是否要跳出索引,但是第一级0 [0,1]是DFA。
columns_1_x columns_2_x columns_1_y columns_2_y
one two
0 0 -1.0 2.0 2.0 -1.0
1 2.0 -1.0 1.0 2.0
1 0 1.0 2.0 NaN NaN
0 2 NaN NaN 2.0 1.0
1 1 NaN NaN -1.0 2.0
答案 2 :(得分:0)
pd.concat
然后magic
def f(d, thresh=1):
c = d.gt(thresh).sum(1)
mask = c.gt(0).groupby(level=[1, 2]).transform('all')
return d.assign(counts=c)[mask]
pd.concat({'bar': DFA, 'foo': DFB}, names=['index_0']).pipe(f)
column_1 column_2 counts
index_0 index_1 index_2
bar 0 0 1 2 1
2 2 1 1
1 2 2 1 1
foo 0 0 2 2 2
2 2 2 2
1 2 1 2 1
def f(d, thresh=1):
# count how many are greater than a threshold `thresh` per row
c = d.gt(thresh).sum(1)
# find where `counts` are > `0` for both dataframes
# conveniently dropped into one dataframe so we can do
# this nifty `groupby` trick
mask = c.gt(0).groupby(level=[1, 2]).transform('all')
# \-------/
# This is key to broadcasting over
# original index rather than collapsing
# over the index levels we grouped by
# create a new column named `counts`
# /------------\
return d.assign(counts=c)[mask]
# \--/
# filter with boolean mask
# Use concat to smash two dataframes together into one
pd.concat({'bar': DFA, 'foo': DFB}, names=['index_0']).pipe(f)