简体   繁体   English

有没有一种有效的方法来选择大熊猫数据框中的多行?

[英]Is there an efficient way to select multiple rows in a large pandas data frame?

I am working on a large pandas adatframe with about 100 million rows and 2 columns. 我正在研究一个约有1亿行和2列的大熊猫adatframe。 I want to iterate over the dataframe and efficiently set a third column depending on the values of col1 and col2. 我想遍历数据框并根据col1和col2的值有效地设置第三列。 This is what I am currently doing - 这是我目前正在做的-

df[col3] = 0
for idx, row in df.iterrows():
    val1 = row[col1]
    val2 = row[col2]
    df1 = df.loc[(df.col1 == val2) & (df.col2 == val1)]
    if len(df1) > 0:
        df.loc[(df.col1 == val2) & (df.col2 == val1), col3] = 1
Example:
    df = pd.DataFrame({'col1':[0,1,2,3,4,11], 'col2':[10,11,12,4,3,0]})
    >> df.head()
        col1 col2
     0  0   10
     1  1   11
     2  2   12
     3  3   4
     4  4   3
     5  3   10
    I want to add 'col3' such that last 2 rows of the third column are
    1. Think of it as a reverse_edge column which is 1 when for each 
    (val1, val2) in col1, col2 there is a (val2, val1) in col1, col2
        col1    col2    col3
      0 0        10      0
      1 1        11      0
      2 2        12      0
      3 3        4       1
      4 4        3       1
      5 11       0       0

What is the most efficient way to do this computation? 进行此计算的最有效方法是什么? It is currently taking me hours to traverse the entire dataframe. 目前,遍历整个数据框需要花费我几个小时。

EDIT: Think of each value in col1 and corresponding value in col2 as an edge in a graph (val1 -> val2). 编辑:将col1中的每个值和col2中的对应值视为图形中的一条边(val1-> val2)。 I want to know if a reverse edge exists or not (val2 -> val1). 我想知道是否存在反向边缘(val2-> val1)。

My solution would be to merge the frame to itself (merging column 2 to column 1) and then checking if the other two columns would be identical: that would mean the reverse also exists: 我的解决方案是将框架合并到自身(将第2列合并到第1列),然后检查其他两列是否相同:这意味着也存在相反的情况:

df2 = df.merge(df, how='left', left_on='col2', right_on='col1')
df['rev_exists'] = (df2['col1_x'] == df2['col2_y']).astype(int)
df
#   col1  col2  rev_exists
#0     0    10           0
#1     1    11           0
#2     2    12           0
#3     3     4           1
#4     4     3           1
#5    11     0           0

Along the same lines as @Jondiedoop's answer, you can safe a bit of suffix wrangling and stick to an inner join by merging on both columns at once, 与@Jondiedoop的回答一样,您可以安全地进行一些后缀争用并通过一次合并两个列来保持内部联接,

df['col3'] = df.index.isin(df.merge(df, left_on=['col1', 'col2'], right_on=['col2', 'col1'], left_index=True).index).astype(int)

For example: 例如:

In [40]: df
Out[40]:
   col1  col2
0     0    10
1     1    11
2     2    12
3     3     4
4     4     3
5    11     0
6     0    10

In [41]: df['col3'] = df.index.isin(df.merge(df, left_on=['col1', 'col2'], right_on=['col2', 'col1'], left_index=True).index).astype(int)

In [42]: df
Out[42]:
   col1  col2  col3
0     0    10     0
1     1    11     0
2     2    12     0
3     3     4     1
4     4     3     1
5    11     0     0
6     0    10     0

An equivalent approach would be: 一种等效的方法是:

df['col3'] = 0
df.loc[df.merge(df, left_on=['col1', 'col2'], right_on=['col2', 'col1'], left_index=True).index, 'col3'] = 1

Use: 采用:

df1 = pd.DataFrame(np.sort(df[['col1', 'col2']], axis=1), index=df.index)
df['col3'] = df1.duplicated(keep=False).astype(int)
print (df)
   col1  col2  col3
0     0    10     0
1     1    11     0
2     2    12     0
3     3     4     1
4     4     3     1

Another solution with merge and compare subsets, compare to 2d array s, last use np.all for check all True per rows: 另一个解决方案是merge和比较子集,与2d array进行比较,最后使用np.all检查每行np.allTrue

df2 = df.merge(df, how='left', left_on='col2', right_on='col1')

df['col3'] = ((df2[['col1_x','col2_x']].values == 
               df2[['col2_y','col1_y']].values).all(axis=1).astype(int))
#pandas 0.24+
#https://stackoverflow.com/a/54508052
#df['col3'] = ((df2[['col1_x','col2_x']].to_numpy() ==
                df2[['col2_y','col1_y']].to_numpy()).all(axis=1).astype(int))
print (df)
   col1  col2  col3
0     0    10     0
1     1    11     0
2     2    12     0
3     3     4     1
4     4     3     1
5    11     0     0

print ((df2[['col1_x','col2_x']].values == df2[['col2_y','col1_y']].values))


[[False False]
 [False  True]
 [False False]
 [ True  True]
 [ True  True]
 [False  True]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM