Home > Software engineering >  Filter dataframe to get name of the youngest of a particular gender
Filter dataframe to get name of the youngest of a particular gender

Time:05-03

I waste 2 hours and shouldn't find solve to my problem. I need filtering from csv Only name of female who has minimal age.

I do only this part, and don't know how i can combine my solve in one right solve. Can you please support me, and say what an attributes can help me in my problem.

Columns = ['name', 'gender', 'age', 'height', 'weight']

frame = pd.read_csv("h03.csv")
out = pd.DataFrame(data=frame)

filtr = frame[frame['gender'] == 'F']
min_age = filtr['age']
ne = frame.loc[frame.gender == 'F']
ne = frame[frame['age']==frame['age']].min()

print(ne)

CodePudding user response:

Without seeing more of your data this should be a good enough starting point for you to put your own column names and data.

df = pd.DataFrame(
    {
        'Gender':['M', 'F', 'M', 'F', 'M', 'F'],
        'Age':[20, 21, 21, 13, 22, 13]
    }
)
df = df.loc[df['Gender'] == 'F']
df['Check'] = np.where(df['Age'] == df['Age'].min(), True, False)
df = df.loc[df['Check'] == True]
df

CodePudding user response:

df = df.sort_values(by = 'age')

df[df.Gender == 'F'].iloc[0].name

  • Related