通过映射中的词典创建两列时出错

2024-04-26 11:57:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: csvpathmapreaddatabryandictsep
1条回答
网友
1楼 · 发布于 2024-04-26 11:57:11

问题是,根据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

相关问题 更多 >