问题
The pandas documentation of .loc
clearly states:
.loc is strictly label based, will raise KeyError when the items are not found, allowed inputs are:
A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index. This use is not an integer position along the index)
Contrary to that, this surprisingly works for pd.Series, not for pd.DataFrame:
import numpy as np
a = np.array([1,3,1,2])
import pandas as pd
s = pd.Series(a, index=["a", "b", "c", "d"])
s.loc["a"] # yields 1
s.loc[0] # should be strictly label-based, but it works and also yields 1
Do you know why?
来源:https://stackoverflow.com/questions/21574882/why-does-loc-work-with-integer-index-positions