Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么pandas DataFrame中的矢量查找不起作用,但它在日期上与系列/查找一起起作用_Python_Python 2.7_Pandas - Fatal编程技术网

Python 为什么pandas DataFrame中的矢量查找不起作用,但它在日期上与系列/查找一起起作用

Python 为什么pandas DataFrame中的矢量查找不起作用,但它在日期上与系列/查找一起起作用,python,python-2.7,pandas,Python,Python 2.7,Pandas,用于: 查找是否正确: In [39]: x = pd.Series(np.random.randn(6),index=pd.date_range('2015-01-15','2015-01-20')) 有人能解释一下为什么这个系列可以在查找上工作,而在数据帧上却不能 这是x: In [40]: x[datetime(2015,1,15)] Out[40]: -2.0727569075280319 简单的回答是,您正在从不同的轴进行选择。 请参阅索引文档 这将从索引轴进行选择 In [1]

用于:

查找是否正确:

In [39]: x = pd.Series(np.random.randn(6),index=pd.date_range('2015-01-15','2015-01-20'))
有人能解释一下为什么这个系列可以在查找上工作,而在数据帧上却不能

这是x:

In [40]: x[datetime(2015,1,15)]

Out[40]: -2.0727569075280319

简单的回答是,您正在从不同的轴进行选择。 请参阅索引文档

这将从索引轴进行选择

In [1]: df = pd.DataFrame(np.random.randn(6),index=pd.date_range('2015-01-15','2015-01-20'))

In [2]: s = pd.Series(np.random.randn(6),index=pd.date_range('2015-01-15','2015-01-20'))

In [3]: key = datetime.datetime(2015,1,15)
这也一样

In [4]: df.loc[key]
Out[4]: 
0    0.562973
Name: 2015-01-15 00:00:00, dtype: float64
这也是如此(因为它只有一个轴!)

以下是DataFrame的列

In [6]: s[key]
Out[6]: 1.1151835852265839
数据帧上的
getitem
默认情况下在列上选择

In [8]: df.columns
Out[8]: Int64Index([0], dtype='int64')
不要混淆,但当您选择
部分切片时,数据帧
确实允许这种便利(这也可能是
datetime(2015,1,15):
-但它必须是一个片段。其想法是,这是一个时间序列上的常见操作,因此它可以工作(这有点令人困惑,但自pandas开始以来就已经建立了很长时间)

串联工作原理相同

In [13]: df['20150115':]
Out[13]: 
                   0
2015-01-15  0.562973
2015-01-16 -1.112382
2015-01-17  0.279265
2015-01-18 -0.919848
2015-01-19 -1.156900
2015-01-20 -0.887971

[6 rows x 1 columns]
In [6]: s[key]
Out[6]: 1.1151835852265839
In [8]: df.columns
Out[8]: Int64Index([0], dtype='int64')
In [9]: df[0]
Out[9]: 
2015-01-15    0.562973
2015-01-16   -1.112382
2015-01-17    0.279265
2015-01-18   -0.919848
2015-01-19   -1.156900
2015-01-20   -0.887971
Freq: D, Name: 0, dtype: float64
In [13]: df['20150115':]
Out[13]: 
                   0
2015-01-15  0.562973
2015-01-16 -1.112382
2015-01-17  0.279265
2015-01-18 -0.919848
2015-01-19 -1.156900
2015-01-20 -0.887971

[6 rows x 1 columns]
In [15]: s['20150115':]
Out[15]: 
2015-01-15    1.115184
2015-01-16    0.604819
2015-01-17   -0.112881
2015-01-18   -1.234023
2015-01-19    1.264301
2015-01-20   -0.873921
Freq: D, dtype: float64