Alex
Alex

Reputation: 4264

Get rid of columns that do not have headers

I'm trying to read in a CSV file that has columns without headers. Currently, my solution is

df = pd.read_csv("test.csv")
df = df[[col for col in df.columns if 'Unnamed' not in col]]

This seems a little hacky, and would fail if the file contains columns with the word 'Unnamed' in them. Is there a better way to do this?

Upvotes: 3

Views: 760

Answers (2)

Xukrao
Xukrao

Reputation: 8633

The usecols argument of the read_csv function accepts a callable function as input. If you provide a function that evaluates to False for your undesired column headers, then these columns are dropped.

func = lambda x: not x.startswith('Unnamed: ')
df = pd.read_csv('test.csv', usecols=func)

I guess that this solution is not really fundamentally different from your original solution though.

Upvotes: 2

Diego Mora Cespedes
Diego Mora Cespedes

Reputation: 3862

Maybe you could rename those columns first?

df = pd.read_csv("test.csv")
df.columns = df.columns.str.replace('^Unnamed:.*', '')    
df[[col for col in df.columns if col]]

Still pretty hacky, but at least this replaces only the strings which start with "Unnamed:" with '' before filtering them.

Upvotes: 0

Related Questions