Pandas: universal columns attribute for DataFrame and SeriesAsk Question

问题:

I have a function that gets passed a DataFrame and uses the df.columns attribute. However, this breaks when it's passed a DataFrame that has only one column (series), because a series doesn't have that attribute.

Is there an attribute that would work for both a series and a dataframe that would be equivalent to df.columns for a DataFrame ? I've looked, but given that the columns attribute is not even shown in the DataFrame documentation (http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html), I thought there might be a solution out there in the form of some tribal knowledge.

回答1:


use .name for a series, I'd just test the type of the object being passed and call .columns or .name as appropriate:

In [242]:
df = pd.DataFrame(columns=list('abc'))
df

Out[242]:
Empty DataFrame
Columns: [a, b, c]
Index: []

In [243]:
df['a'].name

Out[243]:
'a'

Like this:

In [245]:
def func(x):
    if type(x) is pd.DataFrame:
        print( x.columns)
    elif type(x) is pd.Series:
        print( x.name)

print(func(df))
print(func(df['a']))

Index(['a', 'b', 'c'], dtype='object')
None
a
None
标签: pandas dataframe attributes series
© 2014 TuiCode, Inc.