WindChimes
WindChimes

Reputation: 3103

Saving Pandas dataframe indices to file

I am trying to save just the indices of a dataframe to a file. Here is what I've tried:

A)

np.savetxt("file_name", df.index.values)

returns:

TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

B)

df.index.values.tofile("file_name")

returns:

IOError: cannot write object arrays to a file in binary mode

C)

with open("file_name","w") as f:
    f.write("\n".join(df_1_indexes.values.tolist()))

Can someone please explain why A) and B) failed? I'm at a loss.

Cheers

Upvotes: 3

Views: 12003

Answers (2)

Code42
Code42

Reputation: 2482

In addition to @root's answer, if you've already deleted df after saving its index, you can do this:

saved_index=df.index
pd.Series(saved_index,index=saved_index).to_csv('file_name', header=False, index=False)

Upvotes: 3

root
root

Reputation: 33793

The error in A) is likely because you have strings or someother type of object in your index. The default format specifier in np.savetxt appears to assume the data is float-like. I got around this by setting fmt='%s', though it likely isn't a reliable solution.

B) doesn't yield any errors for me, using some basic examples of Index and MultiIndex. Your error is probably due to the specific type of elements in your index.

Note that there is an easier and more reliable way to save just the index. You can set the columns parameter of to_csv as an empty list, which will suppress all columns from the output:

df.to_csv('file_name', columns=[], header=False)

If your index has a name and you want the name to appear in the output (similar to how column names appear), remove header=False from the code above.

Upvotes: 13

Related Questions