我有一个带有多索引的 Pandas DataFrame
(Index col 1) (Index col 2) (Data col 1) ....
A a word1
a word2
b word3
B a word4
c word5
Run Code Online (Sandbox Code Playgroud)
现在我想计算索引列 1 和索引列 2 具有相同组合的所有行。我试过 df.value_counts(),它给出了错误“DataFrame 没有方法 value_counts()”。如果我使用 df.count(),我只能计算 level=0 或 level=1,不能同时计数(level 参数似乎不接受列表,即使我经常看到在 stackoverflow 上使用) .
期望输出:A a 2 A b 1 .. 等
[编辑]:好的,@EdChum 的评论解决了问题,但我仍然想知道为什么其他东西不起作用?具体来说:为什么 value_counts 似乎没有被定义,而它是最新 Pandas 的一部分?这与我使用 Jupyter Notebook 有什么关系吗?或者这些东西在 Pandas 版本之间变化很大吗?
当我在邮递员中发出此POST请求时,我得到了数据。当我在Python 2.7(使用Jupyter笔记本)中执行此操作时,出现错误“无法解码JSON对象”。我在做什么错,如何使它正常工作?
import json
import requests
url = 'http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy'
headers={'content-type': 'application/json'}
payload = {
"query": [
{
"code": "ContentsCode",
"selection": {
"filter": "item",
"values": [
"BE0101N1"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2010",
"2011"
]
}
},
{
"code": "Region",
"selection": {
"filter": "item",
"values": [
"01"
]
}
}
],
"response": {
"format": "json"
}
}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=payload)
print(r.text)
print(r.json())
Run Code Online (Sandbox Code Playgroud)
api的手册在这里,但是并没有太大帮助:
http://www.scb.se/zh_/About-us/Open-data-API/API-for-the-Statistical-Database-/