2017-01-02 62 views
2

我使用請求庫包裝成一個功能的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'

因此,我的問題是如何正確地應用功能foodf列?

+0

你的函數'foo的定義()'是不是幫助FUL。我用'return json.loads(「[1,2,3,{\」a \「:123}]」)'替換了它的主體 - 錯誤無法複製。 – DyZ

+1

你可以試試這個:'df [['colA']]。apply(foo,axis = 1)' – MaxU

+0

感謝@MaxU的幫助,你的解決方案工作,爲什麼會發生這種情況? – tumbleweed

回答

1

調用df['colA'].apply(foo)是類似於:foo(df['colA'])(其中df['colA'] - 是pandas.Series),所以你的功能應該是能夠接受pandas.Series作爲參數 - 如果不是這種情況,並foo()只能接受標量參數,那麼我們要叫foo()每一行:

df[['colA']].apply(foo, axis=1) 

注:df[['colA']] - 是一個數據幀,作爲Series.apply()函數沒有axis說法

+0

感謝您的解釋...我如何修改我的功能,以接收熊貓列作爲參數? – tumbleweed

+1

@歡迎光臨!您當前的實施已收到整列(系列)。所以它取決於'http:// api.example.com/foo' - 它能夠處理/接受一個值列表(系列)嗎? – MaxU

+0

api默認可以接受一個字符串('nko'參數),我應該如何正確地將數據幀傳遞給'nko'參數? – tumbleweed