在Python中使用熊猫在两个DataFrame之间进行值匹配
作者:互联网
嗨,我有两个像下面的DataFrames
DF1
Alpha | Numeric | Special
and | 1 | @
or | 2 | #
lol ok | 4 | &
DF2 with single column
Content
boy or girl
school @ morn
pyc LoL ok student
Chandra
我想搜索DF1列中的任何人是否在DF2内容列中的任何关键字,并且输出应该在新的DF中
`df11 = (df1.unstack()
.reset_index(level=2,drop=True)
.rename_axis(('col_order','col_name'))
.dropna()
.reset_index(name='val_low'))
df22 = (df2['Content'].str.split(expand=True)
.stack()
.rename('val')
.reset_index(level=1,drop=True)
.rename_axis('idx')
.reset_index())`
df22['val_low'] = df22['val'].str.lower()
df = (pd.merge(df22, df11, on='val_low', how='left')
.dropna(subset=['col_name'])
.sort_values(['idx','col_order'])
.drop_duplicates(['idx']))
df = (pd.concat([df2, df.set_index('idx')], axis=1)
.fillna({'col_name':'Other'})[['val','col_name','Content']])
但它没有考虑大声笑之间的空格
expected_output_DF
val col_name Content
0 or Alpha boy or girl
1 @ Special school @ morn
2 lol ok Alpha pyc LoL ok student
3 NaN Other Chandra
有人帮我这个
解决方法:
使用str.cat str.extract.然后,使用map作为列名,并使用pd.concat加入.
i = df.stack().astype(str)
j = i.reset_index(level=0, drop=1)
m = dict(zip(j.values, j.index))
v = i.str.cat(sep='|')
df2['val'] = df2.Content.str.extract(r'\s(' + v + r')\s', flags=re.I, expand=False)
df2['col_name'] = df2['val'].str.lower().map(m).fillna('Other')
df2
Content val col_name
0 boy or girl or Alpha
1 school @ morn @ Special
2 pyc LoL ok student LoL ok Alpha
3 Chandra NaN Other
细节
> i和j是用于创建映射的设置变量
> m是值到列名称的映射
> v是发送到str.extract进行关键字提取的正则表达式模式.我用re.I忽略了这种情况
>我使用map fillna通过m将提取的值映射到列名
v
'and|1|@|or|2|#|lol ok|4|&'
m
{'#': 'Special',
'&': 'Special',
'1': 'Numeric',
'2': 'Numeric',
'4': 'Numeric',
'@': 'Special',
'and': 'Alpha',
'lol ok': 'Alpha',
'or': 'Alpha'}
df['val']
0 or
1 @
2 LoL ok
3 NaN
Name: val, dtype: object
df['col_name']
0 Alpha
1 Special
2 Alpha
3 Other
Name: col_name, dtype: object
标签:data-analysis,python,pandas,dataframe 来源: https://codeday.me/bug/20191009/1880572.html