我可以更高效地运行此代码吗?这是一个简化的示例,实际上我正在使用一个包含近一百万个条目的表并执行多个循环。运行需要很长时间。欢迎任何建议!
import pandas as pd
codeTable = {'Code': [1,2,3], 'Activity': ['a1', 'a2', 'a3']}
Table = {'Code':[1,2,4,3,6,7,15,2,3,4]}
df1 = pd.DataFrame(data = codeTable)
df2 = pd.DataFrame(data = Table)
df2['Activity'] = "-"
for i in range (0,df2.shape[0]):
if df2['Code'][i] in set(df1['Code']):
df2['Activity'][i] = df1['Activity'][ df1['Activity'][df1['Code'] == df2['Code'][i] ].index [0] ]
else:
df2['Activity'][i] = "-"