简体   繁体   English

如何将 map 一个 dataframe 中的一列字符串值转换为另一个 dataframe 中的另一列?

[英]How do I map a column of string values in one dataframe to another column in another dataframe?

I have two dataframes:我有两个数据框:

Dataframe A contains the following column and values - Dataframe A 包含以下列和值 -

Column 1第 1 列
A;一个; B;乙; C C
A;一个; D; D; E
B;乙; C C

Dataframe B contains the following columns and values - Dataframe B 包含以下列和值 -

Column 1第 1 列 Column 2第 2 栏
A一个 Apple苹果
B Banana香蕉
C C Cat
D D Dog
E Egg

Now, I want to map the values in Column 1 of Dataframe 2 to the values in Column 1 of Dataframe 1 to obtain the following column in Dataframe 1: Now, I want to map the values in Column 1 of Dataframe 2 to the values in Column 1 of Dataframe 1 to obtain the following column in Dataframe 1:

Column 1第 1 列 Derived Column派生列
A;一个; B;乙; C C Apple;苹果; Banana;香蕉; Cat
A;一个; D; D; E Apple;苹果; Dog;狗; Egg
B;乙; C C Banana;香蕉; Cat

My first thought was to iterate through each row in Dataframe 1, split the value in column 1 by ';', and then map it to Dataframe 2, but I have ~100k rows in Dataframe 1, and ~10k rows in Dataframe 2 which would make this computationally expensive. My first thought was to iterate through each row in Dataframe 1, split the value in column 1 by ';', and then map it to Dataframe 2, but I have ~100k rows in Dataframe 1, and ~10k rows in Dataframe 2 which会使这在计算上变得昂贵。 Is there a much faster way to do this?有没有更快的方法来做到这一点? Thanks!谢谢!

Try with series.replace with regex=True after creating a dict from the second df:从第二个 df 创建字典后,尝试使用series.replaceregex=True

df1['Column 2'] = df1['Column 1'].replace(df2.set_index('Column 1')
                                           ['Column 2'],regex=True)

print(df1)

  Column 1            Column 2
0  A; B; C  Apple; Banana; Cat
1  A; D; E     Apple; Dog; Egg
2     B; C         Banana; Cat

暂无
暂无

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

相关问题 如何在DataFrame中使用另一列中的值减去一列中的值? - How do I Substract values in one column with the values of another in a DataFrame? 如何将列值从一个 dataframe 提取到另一个? - How do I extract column values from one dataframe to another? 一个 dataframe 的 Map 索引到另一个 dataframe 的列 - Map index of one dataframe to column of another dataframe 如何检查一个数据帧中的列值是否可用或不检查另一数据帧的列中的值? - How to check values of column in one dataframe available or not in column of another dataframe? 如何将一个数据帧的列值附加到另一个数据帧的列 - How to append column values of one dataframe to column of another dataframe 如果使用熊猫在另一个数据帧中不存在列值,如何将它们从一个数据帧合并到另一个数据帧 - How do I merge column values from one dataframe to another if they are not present in another using pandas 比较另一列 dataframe 中一列的值 dataframe - Compare values of one column of dataframe in another dataframe 如何通过将python中的目标列的多列与另一数据帧中的一列进行匹配来更新数据框中的目标列的某些值? - How do I update some values of my target column in a dataframe by matching its multiple columns with one column in another dataframe in python? 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何根据公共列将 dataframe 的列值替换为另一个 dataframe 的值? - How do I replace column values of a dataframe with values of another dataframe based on a common column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM