suresh_chinthy
suresh_chinthy

Reputation: 377

Store nth row elements in a list panda dataframe

I am new to python.Could you help on follow I have a dataframe as follows. a,d,f & g are column names. dataframe can be named as df1

a   d   f   g
20  30  20  20
0   1   NaN NaN

I need to put second row of the df1 into a list without NaN's. Ideally as follows.

x=[0,1]

Upvotes: 2

Views: 1226

Answers (2)

Kraxi
Kraxi

Reputation: 103

Check itertuples()

So you would have something like taht:

for row in df1.itertuples():
   row[0] #-> that's your index of row. You can do whatever you want with it, as well as with whole row which is a tuple now.

you can also use iloc and dropna() like that:

row_2 = df1.iloc[1].dropna().to_list()

Upvotes: 0

Shubham Sharma
Shubham Sharma

Reputation: 71689

Select the second row using df.iloc[1] then using .dropna remove the nan values, finally using .tolist method convert the series into python list.

Use:

x = df.iloc[1].dropna().astype(int).tolist()  
# x = [0, 1]

Upvotes: 3

Related Questions