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


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)

我看了其他的例子,发现这可以用同样的方法完成。我不知道为什么我得到一个错误。任何帮助都会很感激。谢谢!

county_station_data.head()
>>  Counties     Recommended_Station
0   Appling            Waycross
1  Atkinson                Adel
2     Bacon            Sterling
3     Baker             Camilla
4   Baldwin       Milledgeville

问题是,根据字典的映射产生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

最新更新