'The drop function in pandas is not recognizing my index
So, I am posting a copy of my code below. its pretty simple because Im still learning. I have read a bunch of things online, but I cant figure out where my error is coming from. If you have any suggestions, I would love to hear them.
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sn
data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],
'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],
'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(data, index = labels)
print(df)
df = df.append({'animal':'monkey', 'age':5, 'visits':1,'priority':'yes'}, ignore_index = True)
labels.append('k')
df.set_index(pd.Series(labels))
print(df)
df = df.drop('k')
print(df)
"""
I get the error when I try to drop row k. Any help is greatly appreciated
Solution 1:[1]
You have a problem with setting the idex not dropping it. A correct line when you set the index with 'k' included is:
...
df = df.set_index(pd.Series(labels))
...
note that set_index does not change the df it is applied to, it returns a new df. Although you can force the change by setting the argument inplace = True in set_index.
Also probably a slightly better style is to use pd.Index not Series:
df = df.set_index(pd.Index(labels))
Solution 2:[2]
You need to either add inplace=True to set_index or reassign the return value of set_index back to df:
This:
df.set_index(pd.Series(labels), inplace=True)
Or this:
df = df.set_index(pd.Series(labels))
Output:
>>> df
animal age visits priority
a cat 2.5 1 yes
b cat 3.0 3 yes
c snake 0.5 2 no
d dog NaN 3 yes
e dog 5.0 2 no
f cat 2.0 3 no
g snake 4.5 1 no
h cat NaN 1 yes
i dog 7.0 2 no
j dog 3.0 1 no
k monkey 5.0 1 yes
Now df.drop('k') will work:
>>> df = df.drop('k')
animal age visits priority
a cat 2.5 1 yes
b cat 3.0 3 yes
c snake 0.5 2 no
d dog NaN 3 yes
e dog 5.0 2 no
f cat 2.0 3 no
g snake 4.5 1 no
h cat NaN 1 yes
i dog 7.0 2 no
j dog 3.0 1 no
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | piterbarg |
| Solution 2 |
