I have a dataframe with 3 columns.
I save with pd.to_csv(filename)
and then re-open it with
pd.read_csv(filename, index_col=False)
But I get a dataframe with 4 columns, with the left-most column called
Unnamed:0
that is actually just the row number. How can I read the csv without it?
Thanks!
rename( columns={0 :'new column name'}, inplace=True ) . There is no need to use 'Unnamed: 0' , simply use the column number, which is 0 in this case and then supply the 'new column name' . With Pandas 1.0.
An unnamed column in pandas comes when you are reading the CSV file using it. Sometimes we require to drop columns in the dataset that we are not required. It not only saves memory but is also helpful in analyzing the data efficiently.
Stopgap Solution: Filtering with str.That index_col=[0] fix easily solved this annoying problem of 'unnamed:0' and spares code from verbose reinventing the wheel.
if index_col is 0, in this case it means that "1" will be the index of the first column, "2" will be the index for the second column and so on. If you have any other doubts like this you can refer to the pandas documentation.
You should try:
pd.read_csv('file.csv', index_col=0)
index_col : int or sequence or False, default None Column to use as the row labels of the DataFrame. If a sequence is given, a MultiIndex is used. If you have a malformed file with delimiters at the end of each line, you might consider index_col=False to force pandas to not use the first column as the index (row names)
Example Dataset:
I have taken the dataset from google,So while i'm simply trying to import the data with pd.read_csv it shows the Unnamed: 0
as default.
>>> df = pd.read_csv("amis.csv")
>>> df.head()
Unnamed: 0 speed period warning pair
0 1 26 1 1 1
1 2 26 1 1 1
2 3 26 1 1 1
3 4 26 1 1 1
4 5 27 1 1 1
So, Just to avoid the the Unnamed: 0
we have to use index_col=0
and will get the nicer dataframe:
>>> df = pd.read_csv("amis.csv", index_col=0)
>>> df.head()
speed period warning pair
1 26 1 1 1
2 26 1 1 1
3 26 1 1 1
4 26 1 1 1
5 27 1 1 1
Note : So, to make it more explicit to understand when we say index_col=0
, it placed the first column as the index in the dataFrame rather appearing as Unnamed: 0
.
Hope this will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With