这如何在不调用.Series()的情况下从数据帧隐式创建Pandas系列?

问题描述 投票:0回答:2
aapl_table = quandl.get('WIKI/AAPL') # API call to a data source, returns a dataframe object
aapl = aapl_table['Adj. Close']['2017'] # how does this implicitly create a series object?

我已经尝试寻找从Dataframe对象创建Series的方法,但是它们似乎涉及对pd.Series()的调用,在此代码示例中未明确指出。

我最终得到一个系列对象aapl,该对象从带有Adj. Close年份的所有条目中从aapl_table数据框对象中提取2017列值。另外,nameaapl也设置为'Adj. Close'

这是怎么回事,这种从数据框创建系列的方法的文档在哪里?

>>> aapl_table
              Open    High     Low  ...    Adj. Low  Adj. Close  Adj. Volume
Date                                ...                                     
1980-12-12   28.75   28.87   28.75  ...    0.422706    0.422706  117258400.0
1980-12-15   27.38   27.38   27.25  ...    0.400652    0.400652   43971200.0
1980-12-16   25.37   25.37   25.25  ...    0.371246    0.371246   26432000.0
1980-12-17   25.87   26.00   25.87  ...    0.380362    0.380362   21610400.0
1980-12-18   26.63   26.75   26.63  ...    0.391536    0.391536   18362400.0
...            ...     ...     ...  ...         ...         ...          ...
2018-03-21  175.04  175.09  171.26  ...  171.260000  171.270000   35247358.0
2018-03-22  170.00  172.68  168.60  ...  168.600000  168.845000   41051076.0
2018-03-23  168.39  169.92  164.94  ...  164.940000  164.940000   40248954.0
2018-03-26  168.07  173.10  166.44  ...  166.440000  172.770000   36272617.0
2018-03-27  173.68  175.15  166.92  ...  166.920000  168.340000   38962839.0

[9400 rows x 12 columns]
>>> aapl
Date
2017-01-03    114.715378
2017-01-04    114.586983
2017-01-05    115.169696
2017-01-06    116.453639
2017-01-09    117.520300
                 ...    
2017-12-22    175.010000
2017-12-26    170.570000
2017-12-27    170.600000
2017-12-28    171.080000
2017-12-29    169.230000
Name: Adj. Close, Length: 249, dtype: float64
>>> type(aapl_table)
<class 'pandas.core.frame.DataFrame'>
>>> type(aapl)
<class 'pandas.core.series.Series'>
python pandas dataframe series
2个回答
0
投票

之所以发生这种情况,是因为您的索引是日期格式,并且您直接获得年份的列。如果要将结果作为数据框,请尝试使用过滤器方法。


0
投票

Pandas Dataframe对象是Series对象的集合。

第一个代码段访问列'Adj. Close',它是一个Series对象,并将datetime年与2017匹配的行存储到名为aapl的新Series对象中。

aapl_table = quandl.get('WIKI/AAPL') # API call to get data, returns dataframe
aapl = aapl_table['Adj. Close']['2017'] # accesses the 'Adj. Close' Series object in the dataframe

在此代码示例中,没有调用(显式或隐式)pd.Series(),因为Dataframe对象已经是Series对象的集合。

可以看作是Series对象的类似dict的容器。大熊猫的主要数据结构。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

DataFrame |框架[名称] |与名称对应的系列

https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

© www.soinside.com 2019 - 2024. All rights reserved.