Issue
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
Solution
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.
Answered By - root
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.