当我使用pandas DataFrame
,df.loc[:,'A']
或df['A']
从df.A
中选择单个列时,生成的向量是单列DataFrame
而不是Series
。我更喜欢处理Series
,因为我的其余代码使用了Series
特定的方法。
#First, I assign a list of column names to the variable *col_names*
In [1]: col_names = ['TransID', 'BusinessName', 'Timestamp']
#Second, I read in an excel file and assign it to the *Account_Trans* variable
In [2]: Account_Trans = pd.read_excel('Accounts.xlsx',header=None,
skiprows=[0], index_col=False, names=[col_names])
#Lastly, I print the type of three different subset methods with the following output
In [3]: print(type(Account_Trans.iloc[:,3]))
Out [3]: <class 'pandas.core.series.Series'>
In [4]: print(type(Account_Trans.loc[:,'Timestamp']))
Out [4]: <class 'pandas.core.frame.DataFrame'>
In [5]: print(type(Account_Trans['Timestamp']))
Out [5]: <class 'pandas.core.frame.DataFrame'>
我的期望是In [4]
和In [5]
将产生pandas.core.series.Series
的输出类。在Pandas文档中,第5.3.1节(获取)声明以下内容:
“选择一个单列,它产生一个Series,等效于 df.A:“
Section 5.3.1 Getting example of selecting a single dataframe column
我最近更新为pandas 0.23.4
。最近几天,我一直在研究此问题。感谢您提供的任何帮助。