Pandas

Pandas (44)

알 수 없는 사용자 2022. 9. 10. 21:36
728x90

How to select a specific column from a dataframe as a dataframe instead of a series?

# Input
df = pd.DataFrame(np.arange(20).reshape(-1, 5), columns=list('abcde'))

# Solution
type(df[['a']])
type(df.loc[:, ['a']])
type(df.iloc[:, [0]])

# Alternately the following returns a Series
type(df.a)
type(df['a'])
type(df.loc[:, 'a'])
type(df.iloc[:, 1])