Home > Back-end >  write more efficently numpy and pandas code
write more efficently numpy and pandas code

Time:01-31

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

    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

CodePudding user response:

You can simply loc, dropna then tolist :

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

Output :

print(Me)
#[1, -1]
  • Related