通过在pandas中映射字典来创建两列时出错

时间:2015-07-29 17:45:04

标签: python pandas

student_id

我收到以下错误:

county_station_data = pd.read_csv(Reco_Station_Path, sep=',')
dict = {'Bryan South': [27, 0.40000000000000002], 'Lee': [9, 0.71232876712328774], 'Thomas': [4, 0.78846153846153855], 'Twiggs': [3, 0.55319148936170215], 'Ware': [9, 0.58536585365853655], 'Wilkinson': [15, 0.41379310344827586], 'Glascock': [19, 0.39999999999999997], 'Ben Hill': [15, 0.67741935483870974], 'Echols': [23, 0.30769230769230765],.....}
county_station_data['a'], county_station_data['b'] = county_station_data.Counties.map(dict)

我正在浏览其他例子,看到这可以用同样的方式完成。我不知道为什么我会收到错误。任何帮助,将不胜感激。谢谢!

Traceback (most recent call last):
  File "...model/test.py", line 14, in <module>
  county_station_data['a'], county_station_data['b'] = county_station_data.Counties.map(dict)
ValueError: too many values to unpack (expected 2)

1 个答案:

答案 0 :(得分:0)

问题是根据你的dict的映射会产生一列list对象,这在pandas中是不可取的。

import pandas as pd

# example data
# ====================================
df = pd.DataFrame(dict(Counties=list('ABAAB'), val=[1,2,3,4,5]))
dict_map = {'A': [0.1, 0.2], 'B': [0.3, 0.4]}
df.Counties.map(dict_map)

0    [0.1, 0.2]
1    [0.3, 0.4]
2    [0.1, 0.2]
3    [0.1, 0.2]
4    [0.3, 0.4]
Name: Counties, dtype: object

这是一个解决方法,通过定义自定义apply/map函数来解压缩列表。

def func(row):
    global dict_map
    row['a'], row['b'] = dict_map[row['Counties']]
    return row

df.apply(func, axis=1)

  Counties  val    a    b
0        A    1  0.1  0.2
1        B    2  0.3  0.4
2        A    3  0.1  0.2
3        A    4  0.1  0.2
4        B    5  0.3  0.4