When selecting a single column from a pandas DataFrame(say
df.A, etc), the resulting vector is automatically converted to a Series instead of a single-column DataFrame. However, I am writing some functions that takes a DataFrame as an input argument. Therefore, I prefer to deal with single-column DataFrame instead of Series so that the function can assume say df.columns is accessible. Right now I have to explicitly convert the Series into a DataFrame by using something like
pd.DataFrame(df.iloc[:, 0]). This doesn't seem like the most clean method. Is there a more elegant way to index from a DataFrame directly so that the result is a single-column DataFrame instead of Series?
As @Jeff mentions there are a few ways to do this, but I recommend using loc/iloc to be more explicit (and raise errors early if your trying something ambiguous):
In : df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B']) In : df Out: A B 0 1 2 1 3 4 In : df[['A']] In : df[] In : df.loc[:, ['A']] In : df.iloc[:, ] Out[12-15]: # they all return the same thing: A 0 1 1 3
The latter two choices remove ambiguity in the case of integer column names (precisely why loc/iloc were created). For example:
In : df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 0]) In : df Out: A 0 0 1 2 1 3 4 In : df[] # ambiguous Out: A 0 1 1 3
As Andy Hayden recommends, utilizing .iloc/.loc to index out (single-columned) dataframe is the way to go; another point to note is how to express the index positions. Use a listed Index labels/positions whilst specifying the argument values to index out as Dataframe; failure to do so will return a 'pandas.core.series.Series'
A_1 = train_data.loc[:,'Fraudster'] print('A_1 is of type', type(A_1)) A_2 = train_data.loc[:, ['Fraudster']] print('A_2 is of type', type(A_2)) A_3 = train_data.iloc[:,12] print('A_3 is of type', type(A_3)) A_4 = train_data.iloc[:,] print('A_4 is of type', type(A_4))
A_1 is of type <class 'pandas.core.series.Series'> A_2 is of type <class 'pandas.core.frame.DataFrame'> A_3 is of type <class 'pandas.core.series.Series'> A_4 is of type <class 'pandas.core.frame.DataFrame'>