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.
.name for a series, I'd just test the
type of the object being passed and call
.name as appropriate:
In : df = pd.DataFrame(columns=list('abc')) df Out: Empty DataFrame Columns: [a, b, c] Index:  In : df['a'].name Out: 'a'
In : 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