pythonpandas无列名数据合并,Python Pandas-具有不同列的Concat数据框忽略列名称

I have two pandas.DataFrames which I would like to combine into one. The dataframes have the same number of columns, in the same order, but have column headings in different languages. How can I efficiently combine these dataframes?

df_ger

index Datum Zahl1 Zahl2

0 1-1-17 1 2

1 2-1-17 3 4

df_uk

index Date No1 No2

0 1-1-17 5 6

1 2-1-17 7 8

desired output

index Datum Zahl1 Zahl2

0 1-1-17 1 2

1 2-1-17 3 4

2 1-1-17 5 6

3 2-1-17 7 8

The only approach I came up with so far is to rename the column headings and then use pd.concat([df_ger, df_uk], axis=0, ignore_index=True). However, I hope to find a more general approach.

解决方案

If the columns are always in the same order, you can mechanically rename the columns and the do an append like:

Code:

new_cols = {x: y for x, y in zip(df_uk.columns, df_ger.columns)}

df_out = df_ger.append(df_uk.rename(columns=new_cols))

Test Code:

df_ger = pd.read_fwf(StringIO(

u"""

index Datum Zahl1 Zahl2

0 1-1-17 1 2

1 2-1-17 3 4"""),

header=1).set_index('index')

df_uk = pd.read_fwf(StringIO(

u"""

index Date No1 No2

0 1-1-17 5 6

1 2-1-17 7 8"""),

header=1).set_index('index')

print(df_uk)

print(df_ger)

new_cols = {x: y for x, y in zip(df_uk.columns, df_ger.columns)}

df_out = df_ger.append(df_uk.rename(columns=new_cols))

print(df_out)

Results:

Date No1 No2

index

0 1-1-17 5 6

1 2-1-17 7 8

Datum Zahl1 Zahl2

index

0 1-1-17 1 2

1 2-1-17 3 4

Datum Zahl1 Zahl2

index

0 1-1-17 1 2

1 2-1-17 3 4

0 1-1-17 5 6

1 2-1-17 7 8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值