I have titanic data set in python. I need to subset three columns with a filter for each. For Instance, I need a data frame of Name, age > 40, and of only class A passengers. Is there any possibility to apply all filters in one command?
Asked
Active
Viewed 49 times
0
-
https://stackoverflow.com/questions/22591174/pandas-multiple-conditions-while-indexing-data-frame-unexpected-behavior – Aking Jul 14 '22 at 21:32
1 Answers
0
first, you have to import data with those columns whom you want to subset, and then apply filters.
First method
titanic = pd.read_excel("titanic.xlsx", usecols= ["Name", "Age", "Pclass"])
titanic_subdata= titanic[(titanic["Pclass"] ==1) & (titanic["Name"]) & (titanic["Age"] > 40)]
Second method
titanic1 = titanic[["Name", "Sex", "Age"]]
titanic_subdata= titanic1[(titanic1["Pclass"] ==1) & (titanic1["Name"]) & (titanic1["Age"] > 40)]

buddemat
- 4,552
- 14
- 29
- 49