mercredi 19 mai 2021

creat column from existing CSV by using if condition

Here's my data for reference.

https://dl.dropboxusercontent.com/s/ijh78s1bxm7kctv/gp_data.csv -so gp_data.csv https://dl.dropboxusercontent.com/s/1770hoabg1utoat/gp_grades.csv -so gp_grades.csv https://dl.dropboxusercontent.com/s/ymjz6vqjw3jg4e7/ms_data.csv -so ms_data.csv https://dl.dropboxusercontent.com/s/k1howfpyp5rmgnb/ms_grades.csv -so ms_grades.csv

student_data = pd.concat([
    pd.read_csv("gp_data.csv", sep=";", index_col="student_id"),
    pd.read_csv("ms_data.csv", sep=";", index_col="student_id")])
student_grades = pd.concat([
    pd.read_csv("gp_grades.csv", sep=";", index_col="student_id"),
    pd.read_csv("ms_grades.csv", sep=";", index_col="student_id")])
student_df = student_data.join(student_grades, how="inner")
df = student_data.join(student_grades, how="inner")

Qestions are below:

Copy the student dataset into a new dataset. Work on the new dataset.

Create a column called guardianedu and fill it with the student's gardian's education. If the guardian is father, the value of guardianedu will be Fedu, else Medu.

My codes are below:

df = student_df.copy()


df['guardianedu']= (df['Fedu'] if df['guardian'] == 'father') & (df['Medu'] if df['guardian'] == 'mather')

I guess I need to use if condition? because it seems a little bit difficut to do this. Anyone can help me? Thank you!

Aucun commentaire:

Enregistrer un commentaire