且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pandas 中具有相同名称的多个列

更新时间:2023-02-23 22:12:26

相关参数为mangle_dupe_cols

来自文档

mangle_dupe_cols : boolean, default True
    Duplicate columns will be specified as 'X.0'...'X.N', rather than 'X'...'X'

默认情况下,您的所有'a'列均按照上述指定的名称命名为'a.0'...'a.N'.

by default, all of your 'a' columns get named 'a.0'...'a.N' as specified above.

如果您使用的是mangle_dupe_cols=False,则导入此csv会产生错误.

if you used mangle_dupe_cols=False, importing this csv would produce an error.

您可以使用

df.filter(like='a')


演示


demonstration

from StringIO import StringIO
import pandas as pd

txt = """a, a, a, b, c, d
1, 2, 3, 4, 5, 6
7, 8, 9, 10, 11, 12"""

df = pd.read_csv(StringIO(txt), skipinitialspace=True)
df

df.filter(like='a')