Harrison Harrison - 2 months ago 18
Python Question

Skip first row in pandas dataframe when creating list

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?

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']

Answer

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

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:

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