I have a dataframe with a non-unique index.
I want to use .loc on adataframe.
data = [['tom', 10], ['nick', 15], ['juli', 14], ['tom', 12], ['tom', 64]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])
df.set_index('Name', inplace = True)
When I try
df.loc['tom': 'Age']
KeyError: "Cannot get left slice bound for non-unique label: 'tom'"
Why is that? Can I only .loc on dataframes with unique indexes?
CodePudding user response:
You implementation is wrong. Age is not an element of Name index and moreover if you want to slice between rows then rows must be unique. Suppose you can't apply .loc[] on Name index like 'tom':'nick' because Name are not unique.
Check pandas.loc[] for more details.
You could do something like this:
import pandas as pd
data = [['tom', 10], ['nick', 15], ['juli', 14], ['tom', 12], ['tom', 64]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])
df.set_index('Name', inplace = True)
df = df.loc['tom', 'Age']
print(df)
Output:
Name
tom 10
tom 12
tom 64
Name: Age, dtype: int64
