如何映射和压缩两个数据帧python值

2024-05-13 00:53:27 发布

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

一个数据帧作为

first = pd.DataFrame({'Code': [1,2,3,4],'Value': ['Apple','Ornage','Bannana', 'Graps']})

enter image description here

另一个数据帧是

second= pd.DataFrame({'Code': ['1','2','1','2,4','3'],'Product id': ['A','B','C', 'D','E']})

enter image description here

在python编码中,我要求用第三个表列'Required field'中的值替换代码。作为数据帧

third= pd.DataFrame({'Code': ['1','2','1','2,4','3'],'Product id': ['A','B','C', 'D','E'],'Required Field':['Apple(1)','Orange(2)','Apple(1)','Orange(2),Graps(4)','Bannana(3)']})

enter image description here


Tags: 数据idappledataframevaluerequiredcodeproduct
2条回答

尝试:

second["Code"] = second["Code"].str.split(",")
second = second.explode("Code")

first["Code"] = first["Code"].astype(str)
second["Code"] = second["Code"].astype(str)
third = pd.merge(first, second, on="Code")

third["Value"] = third.apply(lambda x: f"{x['Value']}({x['Code']})", axis=1)

print(
    third.groupby("Product id", as_index=False)
    .agg({"Code": ",".join, "Value": ",".join})
    .rename(columns={"Value": "Required Field"})
)

印刷品:

  Product id Code      Required Field
0          A    1            Apple(1)
1          B    2           Orange(2)
2          C    1            Apple(1)
3          D  2,4  Orange(2),Graps(4)
4          E    3          Bannana(3)
>>> df1=pd.DataFrame({"code": [1,2,3,4], "value": ["apple", "orange", "banana", "grapes"]})
>>> df1
   code   value
0     1   apple
1     2  orange
2     3  banana
3     4  grapes


>>> df2=pd.DataFrame({"id":['A','B','C','D','E'], "code": ["1","2","1","2,4","3"]})
>>> df2
  id code
0  A    1
1  B    2
2  C    1
3  D  2,4
4  E    3

转换字符串以列出并映射来自其他数据帧的值:

>>> df2['code']=df2['code'].str.split(',').apply(lambda x: df1.set_index('code').loc[[int(i) for i in x]].value.tolist())
>>> df2
  id              code
0  A           [apple]
1  B          [orange]
2  C           [apple]
3  D  [orange, grapes]
4  E          [banana]

相关问题 更多 >