I have a datafame as such:
#Load the required libraries
import pandas as pd
import matplotlib.pyplot as plt
#Create data_set
data_set = {'id': [1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
2,
2,
3,
3,
3,
3,
3,
3,
3,
4,
4,
4,
],
'cycle': [1,
2,
3,
4,
5,
1,
2,
3,
4,
5,
6,
7,
8,
1,
2,
3,
4,
5,
6,
7,
1,
2,
3,
],
'Salary': [10,
20,
30,
40,
50,
10,
20,
30,
40,
50,
60,
70,
10,
10,
20,
30,
40,
50,
10,
80,
60,
70,
10,
],
'Expenditure': [100,
210,
320,
430,
540,
90,
200,
310,
420,
530,
100,
200,
300,
80,
190,
300,
410,
520,
340,
780,
100,
200,
300,
],
}
#Convert to dataframe
df = pd.DataFrame(data_set)
print("\n df = \n",df)
The above data frame looks as such:
Here id=1 appears 5 times.
Likewise id=2 appears 8 times; id=3 appears 7 times; id=4 appears 3 times
Now, I wish to delete, all the rows from id=2; and id=4.
The result in the dataframe appears such:
Can somebody please let me know how to achieve this task in Python?
Would new_df = df[(df['id'] != 2) & (df['id'] != 4)]
do what you need?