pandas 创建列表时跳过熊猫数据框中的第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39559805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:01:52  来源:igfitidea点击:

Skip first row in pandas dataframe when creating list

pythonpandas

提问by Harrison

I am currently creating a data frame from a specific column in my csv file. I am then creating a list from the values in the data frame, but I would look to skip over the first element in the data frame and not include it in my list. How can I go about doing that?

我目前正在从我的 csv 文件中的特定列创建数据框。然后我从数据框中的值创建一个列表,但我希望跳过数据框中的第一个元素,而不将它包含在我的列表中。我该怎么做呢?

Here's the code that i'm using which is functioning:

这是我正在使用的正在运行的代码:

df = pd.read_csv(filename, header = None, error_bad_lines = False, usecols = [9], names =
['addresses'])
addresses = df['addresses'].tolist()
addresses = [x for x in addresses if str(x) != 'nan']

回答by jezrael

I think you can use indexing[1:]- select all values excluding first:

我认为您可以使用indexing[1:]- 选择除第一项之外的所有值:

addresses = [x for x in addresses[1:] if str(x) != 'nan']

Or:

或者:

addresses = df.loc[1:, 'addresses'].tolist()

Sample:

样本:

df = pd.DataFrame({'addresses':[4,8,7]})
print (df)
   addresses
0          4
1          8
2          7

addresses = df.loc[1:, 'addresses'].tolist()
print (addresses)
[8, 7]

Another solution, thanks Nickil Maveli:

另一个解决方案,感谢Nickil Maveli

import pandas as pd
import io

temp=u"""10
20
30
"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), header=None, skiprows=[0], names=['addresses'])
print (df)
   addresses
0         20
1         30