如何在Pandas DataFrame中选择和删除具有重复名称的列 [英] How to select and delete columns with duplicate name in pandas DataFrame

查看:844
本文介绍了如何在Pandas DataFrame中选择和删除具有重复名称的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的DataFrame,其中某些列具有相同的名称.当我尝试选择存在两次的列(例如del df['col name']df2=df['col name'])时,出现错误.我该怎么办?

I have a huge DataFrame, where some columns have the same names. When I try to pick a column that exists twice, (eg del df['col name'] or df2=df['col name']) I get an error. What can I do?

推荐答案

您可以按索引处理列:

>>> df = pd.DataFrame([[1,2],[3,4],[5,6]], columns=['a','a'])
>>> df
   a  a
0  1  2
1  3  4
2  5  6
>>> df.iloc[:,0]
0    1
1    3
2    5

或者您可以重命名列,例如

Or you can rename columns, like

>>> df.columns = ['a','b']
>>> df
   a  b
0  1  2
1  3  4
2  5  6

这篇关于如何在Pandas DataFrame中选择和删除具有重复名称的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆