Я считаю, что синтаксис предыдущих ответов является избыточным и трудно запоминаемым. Пандас представил метод query()
в v0.13, и я предпочитаю его. Для вашего вопроса вы можете сделать df.query('col == val')
Воспроизводится из http://pandas.pydata.org/pandas-docs/version/0.17.0/indexing.html#indexing-query
In [167]: n = 10
In [168]: df = pd.DataFrame(np.random.rand(n, 3), columns=list('abc'))
In [169]: df
Out[169]:
a b c
0 0.687704 0.582314 0.281645
1 0.250846 0.610021 0.420121
2 0.624328 0.401816 0.932146
3 0.011763 0.022921 0.244186
4 0.590198 0.325680 0.890392
5 0.598892 0.296424 0.007312
6 0.634625 0.803069 0.123872
7 0.924168 0.325076 0.303746
8 0.116822 0.364564 0.454607
9 0.986142 0.751953 0.561512
# pure python
In [170]: df[(df.a < df.b) & (df.b < df.c)]
Out[170]:
a b c
3 0.011763 0.022921 0.244186
8 0.116822 0.364564 0.454607
# query
In [171]: df.query('(a < b) & (b < c)')
Out[171]:
a b c
3 0.011763 0.022921 0.244186
8 0.116822 0.364564 0.454607
Вы также можете получить доступ к переменным в среде, добавив @
.
exclude = ('red', 'orange')
df.query('color not in @exclude')
ConvertAPI имеет библиотеку Python https://github.com/ConvertAPI/convertapi-python , которая поможет вам легко конвертировать pdf в jpg, используя код ниже.
import convertapi
import os
import tempfile
convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
jpg_result = convertapi.convert(
'jpg',
{
'File': 'files/test.pdf',
'ScaleImage': True,
'ScaleProportions': True,
'ImageHeight': 300,
'ImageWidth': 300,
}
)
saved_files = jpg_result.save_files(tempfile.gettempdir())
print("The thumbnail saved to %s" % saved_files)
print(response)
получает код состояния ответа, поэтому он получил 200, что означает, что запрос успешно выполнен
для получения URL, который вы можете использовать .url
print(response.url)
Друг помог мне, с этим IRL, вот оно:
import requests
import json
output = {'output_urls' : []}
url = 'https://v2.convertapi.com/convert/pdf/to/jpg?Secret=xxxxxxx' # Hidden
headers = {'content-type': 'application/json'}
payload = {
'Parameters': [
{
'Name': 'File',
'FileValue': {
'Url': 'to be populated'
}
},
{
'Name': 'StoreFile',
'Value': 'true'
},
{
'Name': 'ScaleImage',
'Value': 'true'
},
{
'Name': 'ScaleProportions',
'Value': 'true'
},
{
'Name': 'ScaleIfLarger',
'Value': 'true'
},
{
'Name': 'ImageHeight',
'Value': '2200'
},
{
'Name': 'ImageWidth',
'Value': '1625'
}
]
}
for x in input_data['input_urls'].split(',') : # input_data is passed by Zapier
if x[-3:] == 'pdf':
payload['Parameters'][0]['FileValue']['Url'] = x
response = requests.post(url, data=json.dumps(payload), headers=headers)
response_obj = json.loads(response._content)
for file_url in response_obj['Files'] :
output['output_urls'].append(file_url['Url'])
elif x[-3:] == 'jpg' or x[-3:] == 'png' or x[-4:] == 'jpeg' :
output['output_urls'].append(x)
return output