简体   繁体   English

如何组合 2 个具有不同形状和不同列名的 DataFrame 列

[英]How to combine 2 DataFrame columns with different shape and also with different columns name

Can someone help me out to combine/Union 2 dataframe who has different shapes with different columns names.有人可以帮我组合/联合 2 dataframe,他有不同的形状和不同的列名。

As you can see 1 dataframe is with 3 columns and the 2nd dataframe contains only 1 column.如您所见,第 1 个 dataframe 包含 3 列,第 2 个 dataframe 仅包含 1 列。

I want to put dataframe 02 clientid column below to dataframe 01 Turtlemint profile_id column and create new column with vertical name and mentioned value as Life against all the value we put under the 1st dataframe columns like I mentioned inside the image. I want to put dataframe 02 clientid column below to dataframe 01 Turtlemint profile_id column and create new column with vertical name and mentioned value as Life against all the value we put under the 1st dataframe columns like I mentioned inside the image.

How can I achieve this?我怎样才能做到这一点?

在此处输入图像描述

Using append while changing the column name of your second dataframe, you can achieve this.在更改第二个 dataframe 的列名时使用append可以实现此目的。

A simple, but similar example:一个简单但类似的例子:

df1 = pd.DataFrame(data=np.array([[1,2,3], ['A', 'B', 'C']]).T, columns=['nr', 'lt'])
df2 = pd.DataFrame(data=['D', 'E', 'F'], columns=['lt2'])
df3 = df1.append(df2.rename(columns={'lt2':'lt'}))
df3.loc[df3['nr'].isnull(), 'Vertical'] = 'Life'

Dataframes:数据框:

df1:
  nr lt
0  1  A
1  2  B
2  3  C

df2:
    nr lt
0    1  A
1    2  B
2    3  C
0  NaN  D
1  NaN  E
2  NaN  F

Output: Output:

    nr lt Vertical
0    1  A      NaN
1    2  B      NaN
2    3  C      NaN
0  NaN  D     Life
1  NaN  E     Life
2  NaN  F     Life

Here I have change the second DF column name same as first DF and then outer joined them, so we can the first DF extra columns.在这里,我将第二个 DF 列名称更改为与第一个 DF 相同,然后将它们外部连接起来,这样我们就可以添加第一个 DF 额外列。

Code:代码:

df1 = pd.concat([df1, df2.rename(columns={'clientid': "Turtlemint profile_id"})], join='outer', axis=0).reset_index(drop=True)

above we haven't changed the second DF column name permeant so still I have use the column name clientid .上面我们没有更改第二个 DF 列名 permeant 所以我仍然使用列名clientid Here the simple logic is if the ID exist in both table give value Life or else None .这里的简单逻辑是如果两个表中都存在 ID,则给出值LifeNone

ADD column:添加栏:

df1['Vertical'] = np.where(df1['Turtlemint profile_id'].isin(df2['clientid']), 'LIFE', None)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM