简体   繁体   English

如何将一列除以另一列,其中一个数据帧的列值对应于 Python Pandas 中另一个数据帧的列值?

[英]How to divide one column by another where one dataframe's column value corresponds to another dataframe's column's value in Python Pandas?

Consider the following data frames in Python Pandas:考虑 Python Pandas 中的以下数据帧:

DataframeA数据框A

ColA可乐 ColB ColB ColC科尔C
1 1 dog 439 439
1 1 cat 932 932
1 1 frog青蛙 932 932
2 2 dog 2122 2122
2 2 cat 454 454
2 2 frog青蛙 773 773
3 3 dog 9223 9223
3 3 cat 3012 3012
3 3 frog青蛙 898 898

DataframeB数据框B

ColD寒冷的 ColE油菜
1 1 101 101
2 2 314 314
3 3 124 124

To note, ColB just repeats it's string values as ColA iterates upwards.需要注意的是,ColB 只是在 ColA 向上迭代时重复它的字符串值。 ColC and ColE are random. ColC 和 ColE 是随机的。 ColA and ColD correspond. ColA 和 ColD 对应。 ColD values will never have repeats (like a map). ColD 值永远不会有重复(如地图)。

I want to divide ColC by ColE wherever ColA == ColD and ideally put the resulting value in a new column in DataframeA (or just have it overwrite ColC).我想在 ColA == ColD 的任何地方将 ColC 除以 ColE ,理想情况下将结果值放在 DataframeA 的新列中(或者只是让它覆盖 ColC)。 The resulting value should be able to have decimals.结果值应该能够有小数。

How can I do this in Python Pandas?如何在 Python Pandas 中执行此操作?

IIUC, you can merge and then divide, just for a new df use below else use @Manakin's answer . IIUC,您可以合并然后划分,仅用于下面的新 df 使用,否则使用@Manakin's answer

out = (df_a.merge(df_b,left_on='ColA',right_on='ColD',how='left')
.assign(new=lambda x:x['ColC'].div(x['ColE'])).reindex(columns=[*df_a.columns]+['new']))

print(out)

   ColA  ColB  ColC        new
0     1   dog   439   4.346535
1     1   cat   932   9.227723
2     1  frog   932   9.227723
3     2   dog  2122   6.757962
4     2   cat   454   1.445860
5     2  frog   773   2.461783
6     3   dog  9223  74.379032
7     3   cat  3012  24.290323
8     3  frog   898   7.241935

you can use .map你可以使用.map

if you have multiple keys to join on then merge would be more useful as demonstrated by @anky如果您有多个键要加入,那么merge会更有用,正如@anky 所证明的那样

df1['ColF'] = df1['ColC'] / df1['ColA'].map(df2.set_index(['ColD'])['ColE'])

ColA   ColB  ColC       ColF
0     1   dog    439   4.346535
1     1   cat    932   9.227723
2     1  frog    932   9.227723
3     2   dog   2122   6.757962
4     2   cat    454   1.445860
5     2  frog    773   2.461783
6     3   dog   9223  74.379032
7     3   cat   3012  24.290323
8     3  frog    898   7.241935

暂无
暂无

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

相关问题 如何使用条件将一个数据框列的值与另一个数据框列的值匹配? - How do you match the value of one dataframe's column with another dataframe's column using conditionals? 切片pandas DataFrame,其中列的值存在于另一个数组中 - Slice pandas DataFrame where column's value exists in another array 将一个数据帧中的列添加到另一个 python pandas - Adding column(s) from one dataframe to another python pandas python pandas:检查数据帧的列值是否在另一个数据帧的列中,然后计数并列出它 - python pandas: Check if dataframe's column value is in another dataframe's column, then count and list it 如何在熊猫数据框中找到与另一列中的多个值相对应的列中具有值的所有行? - How can I find all rows with a value in one column which corresponds to more than one value in another column in a pandas dataframe? Python Pandas:将一列的值检查到另一列 dataframe - Python Pandas: checking value of one column into column of another dataframe 如何根据另一个 dataframe 列的值设置列中的值 - How set value in column according to another dataframe column's value 如果一个数据框的行值在另一数据框的列中,则创建一个新列并获取该索引 - Create a new column if one dataframe's row value is in another data frame's column and get that index 用另一列替换数据框值的一列 - Replace one column of a dataframe's values with another 如何将数据框的值复制到另一个数据框的最后一列/行 - How to copy value of dataframe to another dataframe's last column/row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM