如何在python中完成这样的任务?我得到了一个CSV文件,里面有一堆参数,比如"col">和"para">。所以,我想循环所有参数在"para">列作为一个数组,并得到每个响应数据返回并写入CSV文件?
您需要的是DataFrame的apply()
函数。此函数可以对一列的每个元素执行相同的操作。
如何在pandas中使用apply函数
下面是你的例子:
import pandas as pd
import requests
data = pd.read_csv("input_file.csv")
url = "http://api.com/query?name={}"
data['return'] = data['para'].apply(lambda x:requests.get(url.format(x)).text)
data.to_csv("output_file.csv",index=False)
对于更多的过程,您可以定义一个函数来代替上面的lambda
函数:
import pandas as pd
import requests
import json
def my_process(para):
url = "http://api.com/query?name={}".format(para)
try:
return_json = json.loads(requests.get(url).text)
response = return_json['data'][0]['response'] # get the first response string
except:
response = "" # set a value for the case that the API call has no correct results
return response
data = pd.read_csv("input_file.csv")
data['return'] = data['para'].apply(my_process)
data.to_csv("output_file.csv",index=False)
<ipython-input-29-77b593e201eb> in <module>
11
12 data = pd.read_csv("input.csv")
---> 13 data['return'] = data['para'].apply(my_process)
14
15 data.to_csv("output_file.csv",index=False)
~Anaconda3libsite-packagespandascoreseries.py in apply(self, func, convert_dtype, args, **kwds)
3846 else:
3847 values = self.astype(object).values
-> 3848 mapped = lib.map_infer(values, f, convert=convert_dtype)
3849
3850 if len(mapped) and isinstance(mapped[0], Series):
pandas_libslib.pyx in pandas._libs.lib.map_infer()
<ipython-input-29-77b593e201eb> in my_process(para)
6 url = "http://api.com/query?name={}".format(para)
7 return_json = json.loads(requests.get(url).text)
----> 8 response = return_json['data'][0]['response '] # get the first response string
9 return response
10