索引如何在熊猫中起作用?

use*_*244 9 python pandas

我是python的新手.这似乎是一个要问的基本问题.但我真的想了解这里发生的事情

import numpy as np 
import pandas as pd 
tempdata = np.random.random(5)
myseries_one = pd.Series(tempdata)
myseries_two = pd.Series(data = tempdata, index = ['a','b','c','d','e'])
myseries_three = pd.Series(data = tempdata, index = [10,11,12,13,14])


myseries_one
Out[1]: 
0    0.291293
1    0.381014
2    0.923360
3    0.271671
4    0.605989
dtype: float64

myseries_two
Out[2]: 
a    0.291293
b    0.381014
c    0.923360
d    0.271671
e    0.605989
dtype: float64

myseries_three
Out[3]: 
10    0.291293
11    0.381014
12    0.923360
13    0.271671
14    0.605989
dtype: float64
Run Code Online (Sandbox Code Playgroud)

索引每个数据帧的第一个元素

myseries_one[0] #As expected
Out[74]: 0.29129291112626043

myseries_two[0] #As expected
Out[75]: 0.29129291112626043

myseries_three[0]
KeyError:0 
Run Code Online (Sandbox Code Playgroud)

疑问1:为什么会发生这种情况?为什么myseries_three [0]给我一个keyError?我们的意思是调用myseries_one [0],myseries_one [0]或myseries_three [0]?这种方式调用是否意味着我们通过rownames调用?

疑问2: - Python中的rownames和rownumber与r中的rownames和rownumber不同吗?

myseries_one[0:2]
Out[78]: 
0    0.291293
1    0.381014
dtype: float64

myseries_two[0:2]
Out[79]: 
a    0.291293
b    0.381014
dtype: float64

myseries_three[0:2]
Out[80]: 
10    0.291293
11    0.381014
dtype: float64
Run Code Online (Sandbox Code Playgroud)

疑问3: - 如果调用myseries_three [0]意味着通过rownames调用那么myseries_three [0:3]如何产生输出?myseries_three [0:4]是不是意味着我们正在通过rownumber打电话?请解释和指导.我正在从R迁移到python.所以它对我来说有点混乱.

piR*_*red 8

当你试图切片时myseries[something],something往往是模棱两可的.您正在强调这种含糊不清的案例.在你的情况下,大熊猫试图通过猜测你的意思来帮助你.

myseries_one[0] #As expected
Out[74]: 0.29129291112626043
Run Code Online (Sandbox Code Playgroud)

myseries_one有整数标签.当您尝试使用整数切片时,您打算获取用该整数标记的元素,这是有意义的.事实证明,你有一个标有a的元素,0以便返回给你.

myseries_two[0] #As expected
Out[75]: 0.29129291112626043
Run Code Online (Sandbox Code Playgroud)

myseries_two有字符串标签.你打算用标签0标记所有字符串的标签切换这个系列是不太可能的.所以,pandas假设你的意思是0并且返回第一个元素(感谢pandas,这很有用).

myseries_three[0]
KeyError:0 
Run Code Online (Sandbox Code Playgroud)

myseries_three有整数标签,你试图用整数切片......完美.就让我们该值你... KeyError.哎呀,那个索引标签不存在.在这种情况下,熊猫失败比猜测更安全,也许你想要按位置切片.文档甚至建议如果要消除歧义,请使用loc基于标签的切片和iloc基于位置的切片.

我们试试吧 loc

myseries_one.loc[0]
0.29129291112626043

myseries_two.loc[0]
KeyError:0 

myseries_three.loc[0]
KeyError:0 
Run Code Online (Sandbox Code Playgroud)

只有myseries_one一个标签0.另外两个返回KeyError小号

我们试试吧 iloc

myseries_one.iloc[0]
0.29129291112626043

myseries_two.iloc[0]
0.29129291112626043

myseries_three.iloc[0]
0.29129291112626043
Run Code Online (Sandbox Code Playgroud)

它们都具有相应的位置0并返回第一个元素.


对于范围切片,pandas决定解释性较小,并坚持整数切片的位置切片0:2.记住.实际的真人(编写熊猫代码的程序员)是做出这些决定的人.当您尝试做一些含糊不清的事情时,您可能会得到不同的结果.要消除歧义,请使用lociloc.

iloc

myseries_one.iloc[0:2]

0    0.291293
1    0.381014
dtype: float64

myseries_two.iloc[0:2]

a    0.291293
b    0.381014
dtype: float64

myseries_three.iloc[0:2]

10    0.291293
11    0.381014
dtype: float64
Run Code Online (Sandbox Code Playgroud)

loc

myseries_one.loc[0:2]

0    0.291293
1    0.381014
2    0.923360
dtype: float64

myseries_two.loc[0:2]

TypeError: cannot do slice indexing on <class 'pandas.indexes.base.Index'> with these indexers [0] of <type 'int'>

myseries_three.loc[0:2]

Series([], dtype: float64)
Run Code Online (Sandbox Code Playgroud)