I have a dataframe like below:
data = [['lynda', 10,'F',125,'5/21/2018'],['tom', np.nan,'M',135,'7/21/2018'], ['nick', 15,'F',99,'6/21/2018'], ['juli', 14,np.nan,120,'1/21/2018'],['juli', 19,np.nan,140,'10/21/2018'],['juli', 18,np.nan,170,'9/21/2018']]
df = pd.DataFrame(data, columns = ['Name', 'Age','Gender','Height','Date'])
df
How can I transform dataframe based on np.NaN values of Gender?
I want the original dataframe df to be split into df1(Name,Age,Gender,Height,Date) which will have values of gender(first 3 rows of df)
AND into df2(Name,Age,Height,Date)
which won't have Gender column (last 3 rows of df)
This is one approach:
import pandas as pd
import numpy as np
data = [['lynda', 10,'F',125,'5/21/2018'],['tom', np.nan,'M',135,'7/21/2018'], ['nick', 15,'F',99,'6/21/2018'], ['juli', 14,np.nan,120,'1/21/2018'],['juli', 19,np.nan,140,'10/21/2018'],['juli', 18,np.nan,170,'9/21/2018']]
df = pd.DataFrame(data, columns = ['Name', 'Age','Gender','Height','Date'])
df2 = df[df['Gender'].notnull()].drop("Gender", axis=1)
print(df2)
Output:
Name Age Height Date
0 lynda 10.0 125 5/21/2018
1 tom NaN 135 7/21/2018
2 nick 15.0 99 6/21/2018
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With