Python 熊猫:更有效的索引转储?

Python 熊猫:更有效的索引转储?,python,pandas,Python,Pandas,我有一个熊猫数据框,格式如下: rtt rexb asn country 12345 US 300 0.5 54321 US 150 0.2 12345 MX 160 0.15 我希望转储两个JSON文件:一个包含给定ASN的所有国家的列表,另一个包含给定国家的所有ASN: country-by-asn.json: { "12345": ["US", "MX"], "54321": ["US"] } asn-by-

我有一个熊猫数据框,格式如下:

              rtt rexb
asn   country
12345 US      300 0.5
54321 US      150 0.2
12345 MX      160 0.15
我希望转储两个JSON文件:一个包含给定ASN的所有国家的列表,另一个包含给定国家的所有ASN:

country-by-asn.json:
{
    "12345": ["US", "MX"],
    "54321": ["US"]
}

asn-by-country.json:
{
    "US": ["12345", "54321"],
    "MX": ["54321"]
}
我目前正在做以下工作:

asns = df.index.levels[0]
countries = df.index.levels[1]

country_by_asn = {}
asn_by_country = {}

for asn in asns:
    by_asn = df.loc[[d == asn for d in df.index.get_level_values("asn")]]
    country_by_asn[asn] = list(by_asn.index.get_level_values("country"))

for country in countries:
    by_country = df.loc[[d == country for d in df.index.get_level_values("country")]]
    asn_by_country[country] = list(by_country.index.get_level_values("asn"))
这很管用,但感觉有点笨重。是否有一种更有效的方法(就处理能力而言,不一定就代码复杂性而言)来获得相同的输出

经实验证实为“笨重”。在68000行数据上运行需要435秒

groupby
一起使用,将值转换为
列表
,最后:在2.2秒内对68000行数据进行实验运行

df1 = df.reset_index()

a = df1.groupby('asn')['country'].apply(list).to_json()
b = df1.groupby('country')['asn'].apply(list).to_json()
import json

l = df.index.tolist()

a, b = {}, {}
for x, y in l:
    a.setdefault(x, []).append(y)
    b.setdefault(y, []).append(y)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

from collections import defaultdict

a, b = defaultdict( list ), defaultdict( list )

for n,v in l:
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

a, b = {}, {}

for n, v in l:
    if n not in a:
        a[n] = []
    if v not in b:
        b[v] = []
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)
或者纯python解决方案—首先创建元组列表,然后创建字典,最后在0.06秒内对68000行数据进行实验运行

df1 = df.reset_index()

a = df1.groupby('asn')['country'].apply(list).to_json()
b = df1.groupby('country')['asn'].apply(list).to_json()
import json

l = df.index.tolist()

a, b = {}, {}
for x, y in l:
    a.setdefault(x, []).append(y)
    b.setdefault(y, []).append(y)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

from collections import defaultdict

a, b = defaultdict( list ), defaultdict( list )

for n,v in l:
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

a, b = {}, {}

for n, v in l:
    if n not in a:
        a[n] = []
    if v not in b:
        b[v] = []
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)
类似的解决方案:--在0.06秒内对68000行数据进行了实验运行

df1 = df.reset_index()

a = df1.groupby('asn')['country'].apply(list).to_json()
b = df1.groupby('country')['asn'].apply(list).to_json()
import json

l = df.index.tolist()

a, b = {}, {}
for x, y in l:
    a.setdefault(x, []).append(y)
    b.setdefault(y, []).append(y)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

from collections import defaultdict

a, b = defaultdict( list ), defaultdict( list )

for n,v in l:
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

a, b = {}, {}

for n, v in l:
    if n not in a:
        a[n] = []
    if v not in b:
        b[v] = []
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)
@stevendesu的“新手”解决方案--在0.06秒内实验运行68000行数据

df1 = df.reset_index()

a = df1.groupby('asn')['country'].apply(list).to_json()
b = df1.groupby('country')['asn'].apply(list).to_json()
import json

l = df.index.tolist()

a, b = {}, {}
for x, y in l:
    a.setdefault(x, []).append(y)
    b.setdefault(y, []).append(y)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

from collections import defaultdict

a, b = defaultdict( list ), defaultdict( list )

for n,v in l:
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)
l = df.index.tolist()

a, b = {}, {}

for n, v in l:
    if n not in a:
        a[n] = []
    if v not in b:
        b[v] = []
    a[n].append(v)
    b[v].append(n)

a = json.dumps(a)
b = json.dumps(b)


感谢您提出的解决方案。在一天结束之前,我将对您的每个解决方案(与我的解决方案相比)进行一次快速性能测试,看看它们的运行速度有多快。@pyd-我来晚了,我想pir或其他答案是您需要的。我还没有用我的数据进行测试,一旦我测试了jezrael,我会接受它,对不起delay@pyd-我想你忘了;)这绝对没问题;)天气真好!花了一段时间才找到时间进行测试。使用我的真实数据(48000个ASN和200个国家/地区),我刚刚测量了目前的运行时间。接下来,我将检查数据的准确性(从最快的方法开始):我的方法:435秒,groupby:2.23秒,“纯python”:0.06秒,“相似”:0.06秒