如何加快熊猫数据框中列的匹配速度

时间:2019-03-30 18:04:01

标签: python pandas

我正在尝试在熊猫数据框中找到匹配的值。找到匹配项后,我要对数据框的行执行一些操作。

当前我正在使用此代码:

import pandas as pd

d = {'child_id': [1, 2,5,4], 'parent_id': [3, 4,2,3], 'content':     ["a","b","c","d"]}

df = pd.DataFrame(data=d)

for i in range(len(df)):

        for j in range(len(df)):

            if str(df['child_id'][j]) == str(df['parent_id'][i]):
                print(df.content[i])
            else:
                pass

它工作正常,但速度较慢。由于我要处理具有数百万行的数据集,因此我将花费数月的时间。有更快的方法吗?

编辑:为明确起见,我想创建一个数据框,其中包含匹配内容。

import pandas as pd

d = {'child_id': [1,2,5,4],
 'parent_id': [3,4,2,3],
 'content': ["a","b","c","d"]}

df = pd.DataFrame(data=d)

df2 = pd.DataFrame(columns = ("content_child", "content_parent"))

for i in range(len(df)):

    for j in range(len(df)):

        if str(df['child_id'][j]) == str(df['parent_id'][i]):
            content_child = str(df["content"][i])

            content_parent = str(df["content"][j])

            s = pd.Series([content_child, content_parent], index=['content_child', 'content_parent'])
            df2 = df2.append(s, ignore_index=True)
        else:
            pass

 print(df2)

1 个答案:

答案 0 :(得分:0)

最快的方法是使用numpy的功能:

import pandas as pd


d = {
  'child_id': [1, 2, 5, 4],
  'parent_id': [3, 4, 2, 3],
  'content': ["a", "b", "c", "d"]
}
df = pd.DataFrame(data=d)

comp1 = df['child_id'].values == df['parent_id'].values
comp2 = df['child_id'].values[::-1] == df['parent_id'].values
comp3 = df['child_id'].values == df['parent_id'].values[::-1]

if comp1.any() and not comp2.any() and not comp3.any():
  comp = np.c_[ df['content'].values[comp1] ]
elif comp1.any() and comp2.any() and not comp3.any():
  comp = np.c_[ df['content'].values[comp1], df['content'].values[comp2] ]
elif comp1.any() and comp2.any() and comp3.any():
  comp = np.c_[ df['content'].values[comp1], df['content'].values[comp2], df['content'].values[comp3] ]

print( df['content'].values[comp] )

哪个输出:

[]