Need to write a function that joins two values from two separate columns together of different lengths

Viewed 26

I have a .pkl file that has looks something like this but has at least 300 rows:

X Y Z M
-0.522 3 0.55 Yes
0.44 5 NaN No
NaN NaN 0.241 Maybe
0.325 3 Nan Yes

I want to get a list of values for Y and M [3 = Yes, 5 = No, 3 = Yes ] but in some of the rows there are NaNs.

Currently I am able to get Y without the NaNs But there are no NaNs in M. I need to remove the M values that do not have a Y value. (Y = NaN)

Then print(Y_no_nans together with M_no_Y_nans)

2 Answers

You can do the following (df is your dataframe):

df2 = df[(pd.notna(df['Y']) & (pd.notna(df['M']))]
result = list(zip(df2['Y'], df2['M']))

print(result)

Output

[(3, 'Yes'), (5, 'No'), (3, 'Yes')]

If I'm not mistaken about what you want, you can simply use print(df[["Y","M"]].dropna()). If you wish to remove the NaN from the data altogether, pass an inplace=True argument in the dropna method (df.dropna(inplace=True)).

Related