简体   繁体   中英

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]

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]

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM