简体   繁体   English

使用一个 dataframe 行连接两个不同数据帧的列(熊猫)

[英]Use one dataframe rows to connect the columns of two different dataframes (Pandas)

I have a dataframe named "table":我有一个名为“表”的 dataframe:

UNICO       |   RES    |        
Responsabile|   -      | 
Product     |  Prodotto| 
Brand       |  Brand   | 

The column names of table dataframe corresponds to 2 dataframe.表dataframe的列名对应2个dataframe。 The first one, "Unico", which is empty, is something like this:第一个是“Unico”,它是空的,是这样的:

Responsabile | Product  | Brand
NaN          | NaN      | NaN

The second one, "RES" is something like this:第二个,“RES”是这样的:

Prodotto | Brand
X        | AA
Y        | BB

I want to use the rows of the dataframe table to connect the values of the columns of the dataframe UNICO to the values of the columns of the dataframe RES and populate the dataframe UNICO. I want to use the rows of the dataframe table to connect the values of the columns of the dataframe UNICO to the values of the columns of the dataframe RES and populate the dataframe UNICO. My expected outcome is:我的预期结果是:

UNICO:优尼科:

Responsabile | Product  | Brand
NaN          |     X    | AA
NaN          |     Y    | BB

To solve this situation, there is two main paths:要解决这种情况,主要有两条路径:

  1. Path one: use an outer join, and handle the output to create the desired output.路径一:使用外连接,并处理 output 以创建所需的 output。 The code to perform that is below:要执行的代码如下:
import pandas as pd
import numpy as np

unico = pd.DataFrame({'Resposabile':[np.nan], 'Product':[np.nan], 'Brand':[np.nan]})
res = pd.DataFrame({'Prodotto':['x', 'y'], 'Brand':['AA', 'BB']})

output = pd.merge(left=unico,
                  right=res,
                  how='outer',
                  left_index=True,
                  right_index=True)

output = output[['Resposabile', 'Prodotto', 'Brand_y']]
output.columns = ['Resposabile', 'Prodotto', 'Brand']

This solution implies an outer join.此解决方案意味着外部连接。 Bechas the column Brand appears in both DataFrames, at the end of the day the one retaines is the one with non-nulls values.因为 Brand 列出现在两个 DataFrame 中,最终保留的是具有非空值的列。

  1. Path two (the easiest one): using the pd.concat() function.路径二(最简单的一种):使用 pd.concat() function。 To do that, the syntaxis is in the code below:为此,语法位于以下代码中:
output = pd.concat([unico.Resposabile, res], axis=1)

暂无
暂无

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

相关问题 如何使用两个 Pandas 数据帧创建一个新数据帧,其中包含来自一个数据帧的特定行? - How can I use two pandas dataframes to create a new dataframe with specific rows from one dataframe? 如何创建一个包含 2 个数据帧的 Pandas 数据帧,一个作为列,一个作为行 - How to create a pandas dataframe with 2 dataframes one as columns and one as rows 将文本列拆分为 Pandas DataFrame 中的两列,用于不同的数据帧 - Split text columns into two columns in Pandas DataFrame, for different dataframes 合并两个具有共同值的 Pandas 数据帧,这些值在一个数据帧中显示为列,而在另一个数据框中显示为行 - Merging two pandas dataframes with common values that are presented in one dataframe as columns and on the other are in rows 基于两个不同列数的DataFrame创建二进制pandas DataFrame - Create binary pandas DataFrame based on two DataFrames with different number of columns 将 Pandas 具有不同列的 DataFrame 转换为一个可迭代对象并改革为一个 DataFrame - Convert Pandas DataFrames with different columns into an iterable and reform as one DataFrame 仅对于某些行,两个大小不同的熊猫数据帧的列总和 - Sum columns of two pandas dataframes of different sizes only for certain rows 比较多个列以获取两个Pandas Dataframe中不同的行 - Compare Multiple Columns to Get Rows that are Different in Two Pandas Dataframes 比较两个不同大小的数据帧的各种(但不是全部)列,并从一个数据帧中仅选择条件为真的那些行 - Comparing various (but not all) columns of two different sized dataframes and select only those rows from one dataframe where the conditions are true 计算 Pandas Dataframe 中两个不同列的新行 - Calculating new rows in a Pandas Dataframe on two different columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM