Python熊猫:将选定的列保留为Series而不是DataFrame

时间:2018-08-14 18:49:54

标签: python python-3.x pandas dataframe

当我使用pandas DataFramedf.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。最近几天,我一直在研究此问题。感谢您提供的任何帮助。

0 个答案:

没有答案