Reputation: 4264
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
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
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