2017-04-20 76 views
1

我试图将几个列重新格式化为字符串(它们包含NaN,所以我不能只以整数读取它们)。所有的列目前都是float64,我想这样做,所以他们没有小数。熊猫:列必须与密钥长度相同

下面是数据:

{'crash_id': {0: 201226857.0, 
    1: 201226857.0, 
    2: 2012272611.0, 
    3: 2012272611.0, 
    4: 2012298998.0}, 
'driver_action1': {0: 1.0, 1: 1.0, 2: 29.0, 3: 1.0, 4: 3.0}, 
'driver_action2': {0: 99.0, 1: 99.0, 2: 1.0, 3: 99.0, 4: 99.0}, 
'driver_action3': {0: 99.0, 1: 99.0, 2: 99.0, 3: 99.0, 4: 99.0}, 
'driver_action4': {0: 99.0, 1: 99.0, 2: 99.0, 3: 99.0, 4: 99.0}, 
'harmful_event1': {0: 14.0, 1: 14.0, 2: 14.0, 3: 14.0, 4: 14.0}, 
'harmful_event2': {0: 99.0, 1: 99.0, 2: 99.0, 3: 99.0, 4: 99.0}, 
'harmful_event3': {0: 99.0, 1: 99.0, 2: 99.0, 3: 99.0, 4: 99.0}, 
'harmful_event4': {0: 99.0, 1: 99.0, 2: 99.0, 3: 99.0, 4: 99.0}, 
'most_damaged_area': {0: 14.0, 1: 2.0, 2: 14.0, 3: 14.0, 4: 3.0}, 
'most_harmful_event': {0: 14.0, 1: 14.0, 2: 14.0, 3: 14.0, 4: 14.0}, 
'point_of_impact': {0: 15.0, 1: 1.0, 2: 14.0, 3: 14.0, 4: 1.0}, 
'vehicle_id': {0: 20121.0, 1: 20122.0, 2: 20123.0, 3: 20124.0, 4: 20125.0}, 
'vehicle_maneuver': {0: 3.0, 1: 1.0, 2: 4.0, 3: 1.0, 4: 1.0}} 

当我尝试那些列转换为字符串,这是发生了什么:

>> df[['crash_id','vehicle_id','point_of_impact','most_damaged_area','most_harmful_event','vehicle_maneuver','harmful_event1','harmful_event2','harmful_event3','harmful_event4','driver_action1','driver_action2','driver_action3','driver_action4']] = df[['crash_id','vehicle_id','point_of_impact','most_damaged_area','most_harmful_event','vehicle_maneuver','harmful_event1','harmful_event2','harmful_event3','harmful_event4','driver_action1','driver_action2','driver_action3','driver_action4']].applymap(lambda x: '{:.0f}'.format(x)) 

File "C:\Users\<name>\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2376, in _setitem_array 
     raise ValueError('Columns must be same length as key') 

ValueError: Columns must be same length as key 

我从来没有见过这个错误之前,感觉像这样是简单的...我做错了什么?

+1

,你可以使用'data.to_dict提供样品()',使一些人可以尝试轻松重现 – muon

+1

固定!谢谢,我不知道这是可能的! – ale19

回答

1

你的代码为你提供的字典为我运行。尝试创建一个函数来分别处理NaN情况;我认为他们正在造成你的问题。

一些基本的东西象下面这样:

def formatter(x): 
    if x == None: 
     return None 
    else: 
     return '{:.0f}'.format(x) 
相关问题