Since your df
is just using a default range index, I suggest ignoring the index column with index=False
when saving:
df.to_csv('data.csv', index=False)
Otherwise whenever someone tries to load your data with read_csv('data.csv')
, they'll run into the common and annoyingget an "Unnamed: 0" column that just duplicates the index:
>>> pd.read_csv('data.csv')
# Unnamed: 0 name price link stock
# 0 0 A Light in the Attic £51.77 https://books.toscrape.com/a-light-in-the-atti... In stock
# 1 1 Tipping the Velvet £53.74 https://books.toscrape.com/tipping-the-velvet_... In stock
# .. ... ... ... ... ...
This is a common/annoying issue:
How to get rid of "Unnamed: 0" column in a pandas DataFrame read in from CSV file?
I have a situation wherein sometimes when I read a csv from df, I get an unwanted index-like column named Unnamed: 0.
This is very annoying! Does anyone have an idea on how to get rid of this?
Yes, theyfuture users can fixwork around it by specifying read_csv('data.csv', index_col=0)
, but it's just an unnecessary annoyance that can be circumvented at save timeprevented from the start by saving it once properly.
Also some formatting nits:
- Don't use spaces around default params (e.g.,
class_ = 'product_pod'
->class_='product_pod'
) - Don't mix single/double quotes unless needed (e.g.,
"lxml"
is double while everything else is single) - Use a space after commas (e.g.,
range
anddata.append
have some missing spaces)