简体   繁体   English

根据其他df的值计算新列值

[英]Calculate new column value based on values from other df

I have two data-frames. 我有两个数据帧。
First contains some info based on index like below:(First column is the index) 首先包含一些基于索引的信息,如下所示:(第一列是索引)

index,Dist,Individual
Big,100,50
Small,50,100

second dataframe: 第二个数据帧:

id,hour,machinesize,type 
1,10,Big,Dist
2,20,Small,Individual

I want to calculate the values like below: 我想计算如下的值:

hour* (values from df1 based on Big,Dist ) ie 10 *100 小时*(来自df1的值基于Big,Dist )即10 * 100
My final output will look like below: 我的最终输出如下所示:

id,hour,calc
1,10,1000
2,20,2000  

The 'machinesize' value = 'index' in first df 第一个df中的'machinesize'值='index'

This will get you the desired result: 这将获得您想要的结果:

result = df2.merge(df1, left_on='machinezise', right_on='index')

result.assign(calc=result.lookup(result.index, result.type)*result.hour)[['id', 'hour', 'calc']]

result

   id  hour  calc
0   1    10  1000
1   2    20  2000

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

相关问题 Pandas:将基于具有多个值 map 的其他列的 df 列添加到相同的新列值 - Pandas: Adding a df column based on other column with multiple values map to the same new column value 根据列条件从以前的 df 中提取值到新的 df - extract value from previous df to new df based on column criteria 根据其他列中的值将新列添加到 df - Add new column to df based on values in other columns Python - 基于其他列的几何系列的新 DF 列 - Python -New DF column based on geometric series from other columns (Python)根据来自多个其他列的值在 df 中创建列 - (Python) Creating a column in a df based on values from multiple other columns 根据df中行中的列值和其他行中的列值创建新列? - Creating new column based on column values in row and column values in other rows in df? Python 根据条件和其他列的值计算新列 - Python calculate a new column based on conditions and values of other columns 创建一列 df[New_column] 并根据 Pandas 中的其他多列值写入 Yes/No - Create a column df[New_column] and write Yes/No based on other multiple column values in pandas 根据另一个 df 中的值填充新 df 列中的值 - Filling values in a new df column based on values in another df 通过与其他 df 列进行比较,从一个 df 列的值列表中提取值 - Extract value from list of values in one df column by comparing with other df column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM