Assign dtype with from_dict

I have data in a python dictionary, for example:

data = {u'01-01-2017 22:34:43:871': [u'88.49197', u'valid'], u'01-01-2017 11:23:43:803': [u'88.49486', u'valid'], u'02-01-2017 03:11:43:898': [u'88.49773', u'valid'], u'01-01-2017 13:54:43:819': [u'88.50205', u'valid']} 

I can convert it to pandas Dataframe with:

 data = pandas.DataFrame.from_dict(data, orient='index') 

but I cannot use the dtype from_dict parameter. I would convert the index to a datetime same first column for float and third to a row.

I tried:

 pandas.DataFrame.from_dict((data.values()[0]), orient='index', dtype={0: 'float', 1:'str'}) 

but that will not work.

+5
source share
1 answer

This is apparently a problem with some pandas constructors: How to set dtypes by column in pandas DataFrame

Instead of using the dtype argument, the dtype chain can do the trick:

 pandas.DataFrame.from_dict(data, orient='index').astype({0: float, 1:str}) 
+2
source

Source: https://habr.com/ru/post/1264088/


All Articles