简体   繁体   English

重命名列后如何合并 pandas 数据框?

[英]How to merge pandas dataframes after renaming columns?

I have a code that merged 2 dataframes that had all columns in uppercase.我有一个代码合并了 2 个数据帧,这些数据帧的所有列都是大写的。 I need to adjust the code to merge the dataframes but now 1 comes with columns in lowercase and the other doesn't.我需要调整代码以合并数据框,但现在 1 带有小写的列,而另一个没有。

I wrote the following code to change the columns names to lowercase, and then changed the merge to lowercase also, but no i get a Key error: 'code'我编写了以下代码将列名称更改为小写,然后也将合并更改为小写,但没有出现关键错误:“代码”

df_small = df_small.rename(columns=str.lower)
common = df_small.merge(df_big,on=['code'])

First to 'lower' the column names you can use:首先“降低”您可以使用的列名:

df_small.columns = df_small.columns.str.lower()

I think that your code for lowercasing the column names is doing something wrong.我认为您将列名小写的代码做错了。 Try mine above试试我上面的

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

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