我正在向api发出请求:
def get_data(text, url='api.com'):
r = requests.get(url,
params={'key': '<My KEY>',
'hj': text
'oi': 'm'})
json_data = json.dumps(r.json())
data = yaml.load(json_data)
return data
然后我按照以下方式应用该函数,因为我的数据是在pandas数据帧中:
data
0 The quick fox jumps over the lazy
1 The quick fox over the lazy dog
2 The quick brown fox jumps over the lazy dog
....
n The brown fox jumps over the dog
然后:
df['col'] = df[['data']].apply(get_data, axis=1)
我发送和接收请求的数据的大小非常大,因此如何通过块来提出上述请求?让4比4说出来?:
for chunk in r.iter_content(chunk_size=5):
json_data = json.dumps(r.json())
data = yaml.load(json_data)
return data
然而它不起作用,是否有人可以帮助我通过块分块请求或分块并连接所有内容?
更新
我还尝试按块分割数据帧,但它还没有完成:
在:
df.groupby(np.arange(len(df))//10)
for k,g in df.groupby(np.arange(len(df))//10):
[g.data.apply(get_data) for _, g in df.groupby(np.arange(len(df))//10)]
输出:
----> 7 df = pd.concat(g.data.apply(get_data) for _, g in df2.groupby(np.arange(len(df2))//4))
8 df
/usr/local/lib/python3.5/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
2290 else:
2291 values = self.asobject
-> 2292 mapped = lib.map_infer(values, f, convert=convert_dtype)
2293
2294 if len(mapped) and isinstance(mapped[0], Series):
pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:63307)()
<ipython-input-28-329dbdbb7cdb> in get_data(data)
62
63 r = requests.get('http://api.example.com/api', params=payload, stream = True)
---> 64 json_data = json.dumps(r.json())
65 data = yaml.load(json_data)
66
/usr/local/lib/python3.5/site-packages/requests/models.py in json(self, **kwargs)
848 # used.
849 pass
--> 850 return complexjson.loads(self.text, **kwargs)
851
852 @property
/usr/local/lib/python3.5/site-packages/simplejson/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, use_decimal, **kw)
514 parse_constant is None and object_pairs_hook is None
515 and not use_decimal and not kw):
--> 516 return _default_decoder.decode(s)
517 if cls is None:
518 cls = JSONDecoder
/usr/local/lib/python3.5/site-packages/simplejson/decoder.py in decode(self, s, _w, _PY3)
368 if _PY3 and isinstance(s, binary_type):
369 s = s.decode(self.encoding)
--> 370 obj, end = self.raw_decode(s)
371 end = _w(s, end).end()
372 if end != len(s):
/usr/local/lib/python3.5/site-packages/simplejson/decoder.py in raw_decode(self, s, idx, _w, _PY3)
398 elif ord0 == 0xef and s[idx:idx + 3] == '\xef\xbb\xbf':
399 idx += 3
--> 400 return self.scan_once(s, idx=_w(s, idx).end())
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
但是我不明白在拆分后将所有东西连在一起。
答案 0 :(得分:1)
您可以制作列表理解来存储收到的文件,此处g
将与原始数据框相同但尺寸更小:
[g.data.apply(get_data) for _, g in df.groupby(np.arange(len(df))//10)]
或者你真正想要的是,如果你想对data
系列中的每个文字做出回应:
df.data.apply(get_data)
请注意,df[["data"]]
会返回一个数据框,因此df[["data"]].apply(get_data, axis = 1)
会将整个列传递给get_data
函数。