Skip to content
Advertisement

Save and export dtypes information of a python pandas dataframe

I have a pandas DataFrame named df. With df.dtypes I can print on screen:

JavaScript

I want to save this information so that I can compare it with other data, type-cast things elsewhere, etc. I want to save it into to a local file, recover it elsewhere in another program where the data can’t go. But I’m not able to figure out how. Showing the results of various conversions.

JavaScript

How to save and export/archive dtype information of a pandas DataFrame?

Advertisement

Answer

pd.DataFrame.dtypes returns a pd.Series object. This means you can manipulate it as you would any regular series in Pandas:

JavaScript

Output to csv / excel / pickle

You can then use any method you normally would to store a dataframe, such as to_csv, to_excel, to_pickle, etc. Note for distribution pickle is not recommended as it is version dependent.

Output to json

If you wish to easily store and load as a dictionary, a popular format is json. As you found, you need to convert to str type first:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement