我有以下,非常基本的代码抛出; TypeError:JSON对象必须是str,而不是'bytes'
import requests
import json
url = 'my url'
user = 'my user'
pwd = 'my password'
response = requests.get(url, auth=(user, pwd))
if(myResponse.ok):
Data = json.loads(myResponse.content)
Run Code Online (Sandbox Code Playgroud)
我尝试将解码设置为Data变量,如下所示,但它会抛出相同的错误; jData = json.loads(myResponse.content).decode('utf-8')
有什么建议?
我正在使用Python来解析特定值的一些JSON数据.具体来说,我想提出以下内容:
Python代码看起来像;
import json
import requests
# Set the request parameters
url = 'https:<MYURL.json'
user = 'MY_USER'
pwd = 'MY_PWD'
# Do the HTTP get request
response = requests.get(url, auth=(user, pwd))
# Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Problem with the request. Exiting.')
exit()
# Decode the JSON response
data = response.json()
# Print each value
field_list = data['audits']
for fields in field_list:
print(fields['author_id'])
print(fields['created_at'])
print(fields['events']['public'])
print '\n'
Run Code Online (Sandbox Code Playgroud)
我的代码错误:
File …Run Code Online (Sandbox Code Playgroud) 这是我的数据框:
email title id
---------------------------------
balh@blah.com Title a 123
blah@gmail.com Title b 824
new@blah.com Title a 179
Run Code Online (Sandbox Code Playgroud)
我打电话;
counts = merged_df['title'].value_counts()
Run Code Online (Sandbox Code Playgroud)
返回:
Title a 2
Title b 1
Run Code Online (Sandbox Code Playgroud)
我想要做的是返回所有标题的所有计数的总和。因此,在本例中,我想返回值 3。
我有以下pandas数据帧;
a = [['01', '12345', 'null'], ['02', '78910', '9870'], ['01', '23456', 'null'],['01', '98765', '8760']]
df_a = pd.DataFrame(a, columns=['id', 'order', 'location'])
Run Code Online (Sandbox Code Playgroud)
我需要计算每个ID发生的NULL值(NULL是一个字符串)的数量.结果看起来像;
id null_count
01 02
Run Code Online (Sandbox Code Playgroud)
我可以使用groupby获得基本计数:
new_df = df_a.groupby(['id', 'location'])['id'].count()
Run Code Online (Sandbox Code Playgroud)
但结果返回的不仅仅是NULL值;
id location
01 8760 1
null 2
02 9870 1
Run Code Online (Sandbox Code Playgroud)