简体   繁体   English

根据来自Pandas Python中另外两个单独数据框的列名创建列值

[英]Create columns values based on columns names from another two seperate dataframe in Pandas Python

I have two Pandas Dataframe and I want to be able to create the Result column (yellow) by matching column1 values in first df with the appropriate values from the second df. 我有两个Pandas Dataframe,我希望能够通过将第一个df中的column1值与第二个df中的适当值进行匹配来创建Result列(黄色)。 Column1 values in df one are all references to columns names in the second df. 第一个df中的Column1值都是对第二个df中的列名称的引用。

在此处输入图片说明

在此处输入图片说明

You can use melt and set_index on your df2 您可以在df2上使用melt和set_index

df1 = pd.DataFrame({'Date': ['12/30/2016', '12/30/2016', '1/31/2017', '1/31/2017'], 'col1': ['APB', 'UPB', 'APB', 'UPB']})
df2 = pd.DataFrame({'Date': ['12/30/2016', '1/31/2017', '2/28/2017', '3/31/2017'], 'APB': [117, 112.8, 112.37, 112.23], 'UPB': [67.9, 67.8, 66.7, 66.9]})



df2 = pd.melt(df2, id_vars='Date', value_vars=['APB', 'UPB'])
df2['Date'] = pd.to_datetime(df2['Date'])
df2.sort_values(by = 'Date').set_index('Date')

This gives you 这给你

    variable    value
Date        
2016-12-30  APB 117.00
2016-12-30  UPB 67.90
2017-01-31  APB 112.80
2017-01-31  UPB 67.80
2017-02-28  APB 112.37

Now you can merge the two dataframes, 现在,您可以合并两个数据框,

df1 = df1.merge(df2, left_on = 'col1', right_on = 'variable').drop_duplicates().drop('variable', axis = 1).sort_values(by = 'Date')

That gives you 那给你

    col1    Date    value
0   APB 2016-12-30  117.00
8   UPB 2016-12-30  67.90
1   APB 2017-01-31  112.80
9   UPB 2017-01-31  67.80
2   APB 2017-02-28  112.37
10  UPB 2017-02-28  66.70
3   APB 2017-03-31  112.23
11  UPB 2017-03-31  66.90

As mentioned in the comments, melt and merge are a good approach here: 正如在评论中提到的, 融化合并是一个很好的做法在这里:

import pandas as pd

df1 = pd.DataFrame({'Date': ['12/30/2016', '12/30/2016', '1/31/2017', '1/31/2017'], 
                    'Column1': ['APB', 'UPB', 'APB', 'UPB']})

df2 = pd.DataFrame({'Date': ['12/30/2016', '1/31/2017', '2/28/2017', '3/31/2017'], 
                   'APB': [117, 112.8, 112.37, 112.23], 
                   'UPB': [67.925, 67.865, 66.717, 66.939]})

melted = pd.melt(df2, id_vars="Date", var_name="Column1", value_name="Result")
merged = df1.merge(melted, on=["Date", "Column1"])

print(merged)

  Column1        Date   Result
0     APB  12/30/2016  117.000
1     UPB  12/30/2016   67.925
2     APB   1/31/2017  112.800
3     UPB   1/31/2017   67.865

暂无
暂无

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

相关问题 Pandas:使用基于两列的另一个数据帧中的值替换一个数据帧中的值 - Pandas: replace values in one dataframe with values from another dataframe based on two columns 根据来自另一个数据框的数据将值分配给Pandas数据框中的列 - Assign values to columns in Pandas Dataframe based on data from another dataframe 子集根据另一个数据帧的值在多个列上进行pandas数据帧 - Subset pandas dataframe on multiple columns based on values from another dataframe 根据来自另一个 DataFrame 的值更新 pandas 列中的值 - Update values in pandas columns based on values from another DataFrame Python Pandas:基于两列在dataFrame中创建新行 - Python Pandas: Create new rows in dataFrame based on two columns Pandas 根据另一个数据框中 2 列的值过滤行 - Pandas filter rows based on values from 2 columns in another dataframe 根据 Python 中另一个 dataframe 的行值从 dataframe 中获取列? - Taking columns from a dataframe based on row values of another dataframe in Python? 如何根据条件将来自两个单独列的数据连接到另一个 dataframe? - How to join data from two seperate columns based upon condition to another dataframe? 通过另一个数据框python中的列更改数据框列名称 - Changing dataframe columns names by columns from another dataframe python 熊猫-根据其他数据框列中的值删除列 - Pandas - Remove Columns based on values in another dataframe columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM