【问题标题】:Speed up vlookup like operation using pandas in python在 python 中使用 pandas 加快类似 vlookup 的操作
【发布时间】:2014-02-22 23:19:25
【问题描述】:

我已经编写了一些代码来对两个 pandas 数据帧进行 excel 样式的 vlookup,并希望加快它的速度。

数据帧的结构如下: dbase1_df.columns:
“价值”、“计数”、“网格”、“SGO10GEO”

merged_df.columns:
'GRID'、'ST0'、'ST1'、'ST2'、'ST3'、'ST4'、'ST5'、'ST6'、'ST7'、'ST8'、'ST9'、'ST10'

sgo_df.columns:
'mkey', '类型'

要合并它们,我执行以下操作:
1. 对于 dbase1_df 中的每一行,找到其 'SGO10GEO' 值与 sgo_df 的 'mkey' 值匹配的行。从 sgo_df 中的该行获取“类型”。

  1. 'type' 包含一个范围从 0 到 10 的整数。通过将 'ST' 附加到 type 来创建列名。

  2. 找到merged_df中的值,它的'GRID'值与dbase1_df中的'GRID'值相匹配,并且列名是我们在步骤2中获得的。将这个值输出到一个csv文件中。

//将dbase1 dbf读入数据框

dbase1_df = pandas.DataFrame.from_csv(dbase1_file,index_col=False)
merge_df = pandas.DataFrame.from_csv('merged.csv',index_col=False)

lup_out.writerow(["VALUE","TYPE",EXTRACT_VAR.upper()])
// 对于 dbase1 数据框中的每个唯一值:
对于索引,dbase1_df.iterrows() 中的行:

# 1. Find the soil type corresponding to the mukey
tmp  = sgo_df.type.values[sgo_df['mkey'] == int(row['SGO10GEO'])]
if tmp.size > 0:        
    s_type = 'ST'+tmp[0]
    val       = int(row['VALUE'])            

    # 2. Obtain hmu value
    tmp_val  = merged_df[s_type].values[merged_df['GRID'] == int(row['GRID'])] 
    if tmp_val.size > 0:
        hmu_val = tmp_val[0]             
        # 4. Output into data frame: VALUE, hmu value
        lup_out.writerow([val,s_type,hmu_val])
    else:
        err_out.writerow([merged_df['GRID'], type, row['GRID']])

这里有什么可能是速度瓶颈吗?目前,dbase1_df 中大约 500,000 行大约需要 20 分钟; merge_df 中约 1,000 行,sgo_df 中约 500,000 行。

谢谢!

【问题讨论】:

  • 您可以为您的列提供数据类型吗?我已经准备好了一些东西,但我想先测试一下再放上去。
  • 谢谢,除了merged_df中以'ST'开头的列外,所有都是整数:'ST0,'ST1','ST2','ST3','ST4','ST5','ST6 '、'ST7'、'ST8'、'ST9'、'ST10'。

标签: python performance pandas vlookup


【解决方案1】:

您需要在 Pandas 中使用合并操作以获得更好的性能。我无法测试下面的代码,因为我没有数据,但至少它应该可以帮助你理解:

import pandas as pd

dbase1_df = pd.DataFrame.from_csv('dbase1_file.csv',index_col=False)
sgo_df = pd.DataFrame.from_csv('sgo_df.csv',index_col=False)
merged_df = pd.DataFrame.from_csv('merged_df.csv',index_col=False)

#you need to use the same column names for common columns to be able to do the merge operation in pandas , so we changed the column name to mkey

dbase1_df.columns = [u'VALUE', u'COUNT', u'GRID', u'mkey']

#Below operation merges the two dataframes
Step1_Merge = pd.merge(dbase1_df,sgo_df)

#We need to add a new column to concatenate ST and type
Step1_Merge['type_2'] = Step1_Merge['type'].map(lambda x: 'ST'+str(x))

# We need to change the shape of merged_df and move columns to rows to be able to do another merge operation
id = merged_df.ix[:,['GRID']]
a = pd.merge(merged_df.stack(0).reset_index(1), id, left_index=True, right_index=True)

# We also need to change the automatically generated name to type_2 to be able to do the next merge operation
a.columns = [u'type_2', 0, u'GRID']


result = pd.merge(Step1_Merge,a,on=[u'type_2',u'GRID'])

【讨论】:

  • 谢谢,在最后一行:result = pd.merge(b,a,on=[u'type_2',u'GRID'])。第一个参数“b”是什么?
  • 对不起……我刚刚修好了……这是第一次合并的结果
  • @user308827 这也让它下来了? (从 20 分钟起...)
猜你喜欢
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多