pandas 中具有相同名称的多个列 [英] Multiple columns with the same name in Pandas
问题描述
我正在从CSV文件创建dataframe
.我刚开始Pandas时就浏览了文档,多个SO帖子,链接,但没有理解. CSV文件有多个具有相同名称的列,例如a
.
I am creating a dataframe
from a CSV file. I have gone through the docs, multiple SO posts, links as I have just started Pandas but didn't get it. The CSV file has multiple columns with same names say a
.
那么在形成dataframe
之后,当我执行df['a']
时,它将返回哪个值?它不会返回所有值.
So after forming dataframe
and when I do df['a']
which value will it return? It does not return all values.
此外,只有一个值具有字符串余数的值为None
.如何获得该列?
Also only one of the values will have a string rest will be None
. How can I get that column?
推荐答案
相关参数为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')
这篇关于 pandas 中具有相同名称的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!