Pandas: Setting index = false in function to_csv is not working

Created on 10 Apr 2019  Â·  7Comments  Â·  Source: pandas-dev/pandas

Code Sample, a copy-pastable example if possible

# Your code here
df_ = pd.DataFrame([predicted_labels])
display(df_)
df_.to_csv('person.csv',index=False)

Problem description

Actually I don't want index in my csv file so I set index = false but It's not resolving the issue, my CSV file still has index values.

Expected Output

3,6,0,6,3,0,6,6,6,6,6,0,2,6,6,6,0,0,2,0,0,0,6,0,2,6,6,6,6,6,3,6,3,6,2,0,6,0,6,6,3,2,6,6,6,6

Output of pd.show_versions()

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46
3,6,0,6,3,0,6,6,6,6,6,0,2,6,6,6,0,0,2,0,0,0,6,0,2,6,6,6,6,6,3,6,3,6,2,0,6,0,6,6,3,2,6,6,6,6

Needs Info

Most helpful comment

Hello All,
I have the same issue of df.to_csv("file.csv", index=False) not working.
I found one scenario where it is reproducible.
If behaves just fine before using df.reset_index() but fails after that.

pd.__version__

'0.25.1'

df = pd.DataFrame({
    "A": ["a", "b", "c"],
    "B": [1, 2, 3],
    "C": [9, 8, 7],
    "D": ["x", "y", "z"]})
print(df)

df.to_csv("index_ok.csv", index=False)

df.reset_index(inplace=True)
df.to_csv("index_bad.csv", index=False)
# NOTE: The `inplace=True` is NOT related with the issue. The problem happens regardless.
# df = df.reset_index()
   A  B  C  D
0  1  9  a  x
1  2  8  b  y
2  3  7  c  z



md5-0d12e34a47905c6c2355667c2ec22a72



```shell
$ cat index_bad.csv
index,A,B,C,D
0,1,9,a,x
1,2,8,b,y
2,3,7,c,z

All 7 comments

We would need a more reproducible example. predicted_labels is not defined in your example.

http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports

you can save your dataframe as it is with an index,
and when reading drop index column

df.to_csv(' file_name.csv ')
df_new = pd.read_csv('file_name.csv').drop(['unnamed 0'],axis=1)

closing as not reproducible

Hello All,
I have the same issue of df.to_csv("file.csv", index=False) not working.
I found one scenario where it is reproducible.
If behaves just fine before using df.reset_index() but fails after that.

pd.__version__

'0.25.1'

df = pd.DataFrame({
    "A": ["a", "b", "c"],
    "B": [1, 2, 3],
    "C": [9, 8, 7],
    "D": ["x", "y", "z"]})
print(df)

df.to_csv("index_ok.csv", index=False)

df.reset_index(inplace=True)
df.to_csv("index_bad.csv", index=False)
# NOTE: The `inplace=True` is NOT related with the issue. The problem happens regardless.
# df = df.reset_index()
   A  B  C  D
0  1  9  a  x
1  2  8  b  y
2  3  7  c  z



md5-0d12e34a47905c6c2355667c2ec22a72



```shell
$ cat index_bad.csv
index,A,B,C,D
0,1,9,a,x
1,2,8,b,y
2,3,7,c,z

That looks correct to me. reset_index moves the index into the columns. You
may want drop=True.

On Thu, Oct 17, 2019 at 7:14 AM Angelo Klin notifications@github.com
wrote:

Hello All,
I have the same issue of df.to_csv("file.csv", index=False) not working.
I found one scenario where it is reproducible.
If behaves just fine before using df.reset_index() but fails after that.

pd.__version__

'0.25.1'

df = pd.DataFrame({
"A": ["a", "b", "c"],
"B": [1, 2, 3],
"C": [9, 8, 7],
"D": ["x", "y", "z"]})print(df)

df.to_csv("index_ok.csv", index=False)

df.reset_index(inplace=True)
df.to_csv("index_bad.csv", index=False)# NOTE: The inplace=True is NOT related with the issue. The problem happens regardless.# df = df.reset_index()

A B C D
0 1 9 a x
1 2 8 b y
2 3 7 c z

$ cat index_ok.csv
A,B,C,D
1,9,a,x
2,8,b,y
3,7,c,z

$ cat index_bad.csv
index,A,B,C,D
0,1,9,a,x
1,2,8,b,y
2,3,7,c,z

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/pandas-dev/pandas/issues/26042?email_source=notifications&email_token=AAKAOIVCQVUQGGU33S6CLT3QPBJJPA5CNFSM4HE7SLSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBP4ANQ#issuecomment-543146038,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAKAOIQJYTW2V7LJI7UPDCLQPBJJPANCNFSM4HE7SLSA
.

Hello @TomAugspurger,
Good catch. I missed that minor details.
My original code filters the desired columns early on but uses all columns for the to_csv, so I missed that.

Thanks

df=df1.append(df2)
df.to_csv(file, index=False)
is not working
The file still has index

@TomAugspurger can you please help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ashutosh-Srivastav picture Ashutosh-Srivastav  Â·  3Comments

swails picture swails  Â·  3Comments

nathanielatom picture nathanielatom  Â·  3Comments

scls19fr picture scls19fr  Â·  3Comments

Abrosimov-a-a picture Abrosimov-a-a  Â·  3Comments