Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在DataFrame列中搜索字典键,并在新列中返回字典值_Python_Pandas - Fatal编程技术网

Python 在DataFrame列中搜索字典键,并在新列中返回字典值

Python 在DataFrame列中搜索字典键,并在新列中返回字典值,python,pandas,Python,Pandas,我有一个包含金融机构交易的数据框架。其中一列['vendor_full']是供应商,但它可能包含门店号、实际位置等,因此更难根据供应商的实际身份进行汇总 我已经创建了一个字典,其中键是可能出现在数据框中的供应商名称(或至少是列字符串的一部分),值是供应商名称,因为我希望它被写入一个新列['vendor_short'] 根据@Vaishali的问题和答案,我非常接近解决方案,但区别在于发布上述问题的用户希望使用字典值作为搜索词和返回值。我想搜索键并返回值 将熊猫作为pd导入 数据={'amount

我有一个包含金融机构交易的数据框架。其中一列['vendor_full']是供应商,但它可能包含门店号、实际位置等,因此更难根据供应商的实际身份进行汇总

我已经创建了一个字典,其中键是可能出现在数据框中的供应商名称(或至少是列字符串的一部分),值是供应商名称,因为我希望它被写入一个新列['vendor_short']

根据@Vaishali的问题和答案,我非常接近解决方案,但区别在于发布上述问题的用户希望使用字典值作为搜索词和返回值。我想搜索键并返回值

将熊猫作为pd导入
数据={'amount':[100,150,5,89,55,14],'vendor_full':['store_name 1234','online_store xx55','st_name 9876','杂货店','online_store','clothing_store xx']
cols=['金额','供应商已满']
df=pd.DataFrame(数据,列=cols)
供应商名称={'store_name':'store_name'、'online_store':'online_store'、'st_name':'store_name'、'杂货店'、'网店':'online_store'、'服装店':'服装店'}
pat=r'({})'.format('|'.join(vendor_dict.values()))
cond=df['vendor_full'].str.contains('|'.join(vendor_dict.keys()))
df.loc[cond,'vendor_short']=df['vendor_full'].str.extract((pat),expand=False)
上面的代码似乎适用于第一次出现的供应商,但对于其余出现的供应商,我得到了NaN

实际:

    amount    vendor_full    vendor_short
0   100    store_name 1234   store_name
1   150    online_store xx55 online_store
2   5      st_name 9876      NaN
3   89     grocery_store     grocery_store
4   55     online_shop       NaN
5   14     clothing_store xx clothing_store
预期/期望:

    amount  vendor_full       vendor_short
0   100     store_name 1234   store_name
1   150     online_store xx55 online_store
2   5       st_name 9876      store_name
3   89      grocery_store     grocery_store
4   55      online_shop       online_store
5   14      clothing_store xx clothing_store
方法1 首先,我们用您的dict制作数据框。然后我们提取您的
df
的名称,以便我们可以合并这些名称并获得
供应商\u short

df2 = pd.DataFrame({'vendor_full':list(vendor_dict.keys()),
                    'vendor_short':list(vendor_dict.values())})

s = df['vendor_full'].str.extract("({})".format('|'.join(df2['vendor_full'])))

df['vendor_short'] = s.merge(df2, left_on=0, right_on='vendor_full')['vendor_short']

方法2 使用
.map

s = df['vendor_full'].str.extract("({})".format('|'.join(vendor_dict.keys())))
df['vendor_short'] = s[0].map(vendor_dict)
df['vendor_short'] = df['vendor_full'].str.extract('([a-zA-Z_]+)', expand=False).map(vendor_dict)

方法3 由cs95在评论中提供

使用正则表达式从
vendor\u full
列中提取名称,并使用
.map
将其映射到dict:

s = df['vendor_full'].str.extract("({})".format('|'.join(vendor_dict.keys())))
df['vendor_short'] = s[0].map(vendor_dict)
df['vendor_short'] = df['vendor_full'].str.extract('([a-zA-Z_]+)', expand=False).map(vendor_dict)

df['vendor_full'].str.extract('([a-zA-Z_]+)',expand=False.map(vendor_dict)
   amount        vendor_full    vendor_short
0     100    store_name 1234      store_name
1     150  online_store xx55    online_store
2       5       st_name 9876      store_name
3      89      grocery_store   grocery_store
4      55        online_shop    online_store
5      14  clothing_store xx  clothing_store