简体   繁体   English

在两个不同的pandas Dataframe之间循环和匹配字符串

[英]Loop and match strings between two different pandas Dataframes

I have two different dataframes df and df2, and i want to loop through each row of df in search of certain matches within specific rows in df2 and return a txt file for each match. 我有两个不同的数据帧df和df2,我想循环遍历df的每一行以搜索df2中特定行内的某些匹配,并为每个匹配返回一个txt文件。

df= DF =

 name Tec Location jhon js sr nz mark python ssr us alan java jr mx 

df2= DF2 =

 company job Country Index company a js jr uk 1 company b python ssr us 2 company c java jr mx 3 

So far i've been doing the following: 到目前为止,我一直在做以下事情:

 for index, row in df.iterrows(): for indexb, rowb in df2.iterrows(): if str(row.Tec) in str(rowb.job) and str(row.Location) in rowb.Country: print ('Match with ' + str(rowb.company)) sys.stdout= open(r'path\\to\\file\\%s.txt'%(row['name']+ str(rowb.Index),), 'w') else: pass 

I keep getting text files with wrong matches or with the first row from df2 repeated within all output txt files. 我不断收到错误匹配的文本文件,或者在所有输出txt文件中重复df2的第一行。 I want to compare Tec against job and Location against country. 我想比较Tec与工作和位置对国家。 So for example, an output for this case would be: mark 2.txt , where the file contains the text "Match with company b" 例如,此案例的输出为:mark 2.txt,其中文件包含文本“与公司b匹配”

Any idea ? 任何想法 ?

Iterating rows is not the preferred way of working with Pandas DataFrames. 迭代行不是使用Pandas DataFrames的首选方式。

You might want to try joining df and df2. 您可能想尝试加入df和df2。 Then apply a filter to keep the rows you want to save into csv. 然后应用过滤器将要保存的行保存到csv中。

df = pd.DataFrame([['jhon', 'js sr', 'nz'], ['mark', 'python ssr', 'us'], ['alan', 'java jr', 'mx']], columns=['name', 'Tec', 'Location'])
df2 = pd.DataFrame([['company a', 'js jr', 'uk'], ['company b', 'python ssr', 'us'], ['company c', 'java jr', 'mx']], columns=['company', 'job', 'Country'])
# Merge the two dataframes
df3 = df.merge(df2, how='right', left_on=['Tec', 'Location'], right_on=['job', 'Country'])
df3 = df3[df3['name'].notnull()]
df3['name'].to_csv('output.csv')

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

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