[英]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:要解决这种情况,主要有两条路径:
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 中,最终保留的是具有非空值的列。
output = pd.concat([unico.Resposabile, res], axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.