如何匹配和合并两个数据帧,两个数据帧的值除了数据帧列中的数字外完全不同?

2024-09-27 23:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

具有值的数据帧ABC

      id         |     price                          |   type
0     easdca     | Rs.1,599.00 was trasn by you       | unknown
1     vbbngy     | txn of INR 191.00 using            | unknown
2     awerfa     | Rs.190.78 credits was used by you  | unknown
3     zxcmo5     | DLR.2000 credits was used by you   | unknown

以及其他XYZ值

         price          |   type
0      190.78           | food
1      191.00           | movie
2      2,000            | football
3      1,599.00         | basketball

如何将XYZ与ABC映射,以便使用XYZ价格中的值(数值)将ABC中的type更新为XYZ中的type。你知道吗

我需要的输出

       id         |     price                          |   type
0     easdca     | Rs.1,599.00 was trasn by you        | basketball
1     vbbngy     | txn of INR 191.00 using             | movie
2     awerfa     | Rs.190.78 credits was used by you   | food
3     zxcmo5     | DLR.2,000 credits was used by you| football

用过这个吗

d = dict(zip(XYZ['PRICE'],XYZ['TYPE']))

pat = (r'({})'.format('|'.join(d.keys())))

ABC['TYPE']=ABC['PRICE'].str.extract(pat,expand=False).map(d)

但是像190.78和191.00这样的值越来越不匹配了。 例如,在处理大量数据时,190.78应该与食物值相匹配,比如190.77与食物值不匹配,而食物值被赋予了其他值。198.78也与其他一些应该与食物匹配的不匹配


Tags: 数据youidbytypepriceunknownused
2条回答

测向

        id                price                                type
0       easdca        Rs.1,599.00 was trasn by you          unknown
1       vbbngy        txn of INR 191.00 using               unknown
2       awerfa        Rs.190.78 credits was used by you     unknown
3       zxcmo5        DLR.2000 credits was used by you      unknown

df2型

           price                   type
0        190.78                    food
1        191.00                   movie
2        2,000                 football
3        1,599.00            basketball

使用re

df['price_'] = df['price'].apply(lambda x: re.findall(r'(?<=[\.\s])[\d\.]+',x.replace(',',''))[0])
df2.columns = ['price_','type']
df2['price_'] = df2['price_'].str.repalce(',','')

将类型更改为浮动

df2['price_']  = df2['price_'].astype(float)
df['price_']  = df['price_'] .astype(float)

使用pd.merge

df = df.merge(df2, on='price_')
df.drop('type_x', axis=1)

输出

                id                                 price   price_       type_y
0      easdca        Rs.1,599.00 was trasn by you         1599.00   basketball
1      vbbngy        txn of INR 191.00 using               191.00        movie
2      awerfa        Rs.190.78 credits was used by you     190.78         food
3      zxcmo5        DLR.2000 credits was used by you        2000     football

您可以执行以下操作:

'''
First we make a artificial key column to be able to merge
We basically just substract the floating numbers from the string
And convert it to type float
'''

df1['price_key'] = df1['price'].str.replace(',', '').str.extract('(\d+\.\d+)').astype(float)

# After that we do a merge on price and price_key and drop the columns which we dont need
df_final = pd.merge(df1, df2, left_on='price_key', right_on='price', suffixes=['', '_2'])
df_final = df_final.drop(['type', 'price_key', 'price_2'], axis='columns')

输出

    id      price                               type_2
0   easdca  Rs.1,599.00 was trasn by you        basketball
1   vbbngy  txn of INR 191.00 using             movie
2   awerfa  Rs.190.78 credits was used by you   food
3   zxcmo5  DLR.2000.78 credits was used by you football

我假设您在xyz表中输入了一个错误,第三个价格应该是2000.78,而不是2000。你知道吗

相关问题 更多 >

    热门问题