The following is the data that i am working on the below is the DataFrame in pandas here the index of the rows are string
Openness Conscientiousness Extroversion Agreeableness Neuroticism
Root 0.139050 0.000000 0.325481 0.147281 0.169475
Hips 0.530089 0.477398 0.804277 0.547530 0.686413
Knee 0.869004 1.000000 0.662191 0.936127 1.000000
Ankle 0.964990 0.723205 0.638736 1.000000 0.735433
Toe 0.982430 0.590160 0.851289 0.893272 0.970389
Torso 0.551036 0.372635 0.489920 0.638199 0.611516
Neck 0.000000 0.057604 0.000000 0.000000 0.000000
Head 0.837630 0.502632 0.839598 0.555689 0.797577
Shoulder 0.318785 0.540544 0.844912 0.418395 0.347727
Elbow 0.861450 0.613711 1.000000 0.941450 0.902488
Wrist 0.505890 0.404177 0.477072 0.267725 0.626525
Finger 1.000000 0.708020 0.825508 0.573568 0.757162
When i am doing SET1 is the name of DataFrame
print(SET1.loc('Root'))
I am getting KeyError
ValueError: No axis named Root for object type DataFrame
CodePudding user response:
Short answer, use square brackets with loc:
df.loc['Root'] # using df here in place of SET1
Now there are also cases where you can call loc (using parentheses)
loc is a property than returns a LocIndexer
@property
def loc(self) -> _LocIndexer:
return _LocIndexer("loc", self)
If you look at the code, loc is both callable and indexable/sliceable (and actually more).
The _LocIndexer/_LocationIndexer object returned by loc enable to specify the axis on which to slice/index/…
class _LocationIndexer(NDFrameIndexerBase):
_valid_types: str
axis = None
def __call__(self, axis=None):
# we need to return a copy of ourselves
new_self = type(self)(self.name, self.obj)
if axis is not None:
axis = self.obj._get_axis_number(axis)
new_self.axis = axis
return new_self
Which means you can do:
df.loc(axis=0)['Root']
# OR
df.loc(axis=1)['Root']
to select the axis on which to index
CodePudding user response:
In most programming languages () parenthesis after a name or keyword indicate a call of a function. loc doesn't work like that. You are attempting to access data in a dataframe, that is done in the same way as f you were accessing data in a list or similar data structure. By using [] parenthesis.
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html
In you case you may want to try SET1.loc['Root'] and see where that takes you.
