Harrison Harrison - 4 months ago 31
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 = df['addresses'].tolist()
addresses = [x for x in addresses if str(x) != 'nan']


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

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

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