Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

write more efficently numpy and pandas code

i have a DataFrame

a= pd.read_csv('period_table',index_col=0)
df = pd.DataFrame(a)

enter image description here

i want to add at my var Me only the integer value of a row and i do in this way

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    Me = pd.Series(df.loc[s[0],'ipo_oso':'neg'])
    Me=Me.dropna()
    Me = [int(x) for x in np.array(Me)]

where df is my DataFrame, s[0] is an external variable like ‘H’.
i have used a Series because i don’t know how to remove NaN value. how can i write it with less code and efficently

>Solution :

You can simply loc, dropna then tolist :

Me = df.loc[s[0], "ipo_oso":"neg"].dropna().tolist()

Output :

print(Me)
#[1, -1]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading