我使用請求庫包裝成一個功能的API:不可用類型:'dict'同時應用熊貓功能嗎?
import pandas as pd
import requests, json
def foo(text):
payload = {'key': '00ac1ef82687c7533d54be2e9', 'of': 'json', \
'nko': text, \
'woei': 'm', \
'nvn': 'es'}
r = requests.get('http://api.example.com/foo', params=payload)
data = json.loads(r.text)
return data
然後,我想上面的功能適用於以下數據框:
DF:
colA
0 lore lipsum dolor done
1 lore lipsum
2 done lore
3 dolor lone lipsum
因此,我嘗試了以下內容:
df['new_col'] = df['colA'].apply(foo)
df
但是,我得到了t他以下情況除外:
/usr/local/lib/python3.5/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds) 2287 2288
if is_extension_type(self.dtype): -> 2289 mapped = self._values.map(f) 2290 else: 2291 values = self.asobject/usr/local/lib/python3.5/site-packages/pandas/core/categorical.py in map(self, mapper) 950 return self.from_codes(self._codes.copy(), 951 categories=new_categories, --> 952 ordered=self.ordered) 953 except ValueError: 954 return np.take(new_categories, self._codes)
/usr/local/lib/python3.5/site-packages/pandas/core/categorical.py in from_codes(cls, codes, categories, ordered, name) 466 "codes need to be convertible to an arrays of integers") 467 --> 468 categories = cls._validate_categories(categories) 469 470 if len(codes) and (codes.max() >= len(categories) or codes.min() < -1):
/usr/local/lib/python3.5/site-packages/pandas/core/categorical.py in _validate_categories(cls, categories, fastpath) 571 # categories must be unique 572 --> 573 if not categories.is_unique: 574 raise ValueError('Categorical categories must be unique') 575
pandas/src/properties.pyx in pandas.lib.cache_readonly.get (pandas/lib.c:43685)()
/usr/local/lib/python3.5/site-packages/pandas/indexes/base.py in is_unique(self) 1068 def is_unique(self): 1069 """ return if the index has unique values """ -> 1070 return self._engine.is_unique 1071 1072 @property
pandas/index.pyx in pandas.index.IndexEngine.is_unique.get (pandas/index.c:4883)()
pandas/index.pyx in pandas.index.IndexEngine.initialize (pandas/index.c:5828)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.PyObjectHashTable.map_locations (pandas/hashtable.c:13788)()
TypeError: unhashable type: 'dict'
因此,我的問題是如何正確地應用功能foo
到df
列?
你的函數'foo的定義()'是不是幫助FUL。我用'return json.loads(「[1,2,3,{\」a \「:123}]」)'替換了它的主體 - 錯誤無法複製。 – DyZ
你可以試試這個:'df [['colA']]。apply(foo,axis = 1)' – MaxU
感謝@MaxU的幫助,你的解決方案工作,爲什麼會發生這種情況? – tumbleweed