简体   繁体   English

用from_dict分配dtype

[英]assign dtype with from_dict

I have data in a python dictionary like: 我在python字典中有数据,例如:

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 a pandas Dataframe with: 我可以使用以下方法将其转换为pandas Dataframe:

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

but I am not able to use the dtype parameter of from_dict . 但是我不能使用from_dict参数。 I would convert the index to a datetime of similar first column to float and third to string. 我会将索引转换为类似第一列的datetime时间为float,将第三列转换为字符串。

I have tried: 我试过了:

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

but it doesn't work. 但这不起作用。

This appears to be an ongoing issue with some of the pandas constructor methods: How to set dtypes by column in pandas DataFrame 某些pandas构造函数方法似乎是一个持续存在的问题: 如何通过pandas DataFrame中的列设置dtypes

Instead of using the dtype argument, chaining .astype may do the trick: 而不是使用dtype参数,链接.astype可以做的伎俩:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM