我目前正在从csv文件中的特定列创建数据框。然后我从数据框中的值创建一个列表,但我会跳过数据框中的第一个元素,而不是将它包含在我的列表中。我怎么能这样做?
以下是我正在使用的代码:
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']
答案 0 :(得分:3)
我认为您可以使用indexing
[1:]
- 选择首先排除的所有值:
addresses = [x for x in addresses[1:] if str(x) != 'nan']
或者:
addresses = df.loc[1:, 'addresses'].tolist()
样品:
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]
另一种解决方案,谢谢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