I would like to write a pandas dataframe to a file using a FORTRAN format string. I haven't been able to find anything online except a discussion of how this functionality would be nice. Does anyone know if this is possible?
I suppose I don't need to use a fortran format string...I just need to get the output file in a specific format that fortran can easily read.
UPDATE: For example, I have a large data file that has a specified fortran format. I load the file into my python function, manipulate the data, and then would like to export the manipulated data into a file with the same format it had originally. An example of the file format would be something like:
FORMAT (1X,F12.6,2F9.6,F11.7,T61,2F9.6,F10.7,T142,I6,1X,A2,T236,A1)
The reason I need to export the data in a specific format is because the output file will be read directly into a well-established fortran code (meaning the fortran code cannot be altered).
Update:
I would now do this sort of thing in two steps:
Step 1 -- Convert from pandas dataframe to numpy array or rec-array. This is trivial via the
values
orto_numpy
methods. It's a little trickier if you have strings but see here for one technique. If you have simple numeric data (and no strings), just stick to a regular numpy array and don't bother with a rec-array or structured array.Step 2 -- use numpy's
tofile
to write out a Fortran-readable binaryOriginal Answer:
I guess the bigger question is how to output from pandas to fortran and I'm not sure of the best way, but I'll try to show some fairly simple solutions mainly with
to_csv()
.Doing this will always give you faster IO, and I actually find binary easier than text in this case, although you do lose the ability to view the data as text.
Standard pandas output is actually exactly what you are asking for here, but I'm not sure how to get that into a file except with copy and paste. Maybe there is a way with ipython (though not that I can find).
And here's some default csv output, which is obviously not columnar:
But you may be able to get this into fortran with list directed input.
If you can live with the same format for all numbers, you could do something like this:
A couple notes here: that's not bad but limited in forcing you to use the same format for all numbers, which is pretty wasteful for single digit integers, for example. Also, I tried this with some NaNs and that didn't work very well. And also the commas are not needed there but when I tried to change the separator to ' ', then it quoted everything, so I just left it out.
Finally, the most flexible way might be to convert to strings and format them. This gives you some flexibility to format each column individually. Here's a simple example using a right justified format (and width of 8 for 'x' and 4 for 'y'):
I still can't figure out how to get rid of those commas, but this way does handle NaNs successfully.