简体   繁体   English

在列表中存储第 n 行元素 panda dataframe

[英]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.我是 python 的新手。你能帮忙看看我有一个 dataframe 如下。 a,d,f & g are column names. a,d,f & g 是列名。 dataframe can be named as df1 dataframe可以命名为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 .我需要将 df1 的第二行放入没有NaN's列表中。 Ideally as follows.理想情况如下。

x=[0,1] 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. Select 第二行使用df.iloc[1]然后使用.dropna删除nan值,最后使用.tolist方法将系列转换为 python 列表。

Use:利用:

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

Check itertuples()检查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:你也可以像这样使用ilocdropna()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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