python - 如何将具有多个不同长度值的嵌套列表转换为python中的pandas数据框?

标签 python list dictionary

我从 50 个 Twitter 帐户收集关注者 ID(每个帐户 1,000 到 25,000 个),并且能够以类似于以下的格式将这些关注者 ID 存储在 json 中:


[
    36146779,
    [
        170742628,
        3597763396,
        13453212,
        24763726,
        19087188,
        19605181,
        37374972
    ],
    22971125,
    [
        1114702974,
        1145981566365130758,
        1118409958561685504,
        822439041312423941,
        1110524937788424197,
        807718095460581376,
        24763726,
        3181477874,
        1076870147980300288,
        307465302,
    ],
     24763726,
    [........

我想做的是找到所有相同的关注者ID,比如一个人24763726同时关注帐户36146779和22971125。关于如何解决这个问题有什么建议吗?我对 Python 和一般编程都很陌生,如果有任何帮助或建议,我将非常感激!

到目前为止,我能够将保存的数据(json 格式)转换为 pandas 数据框,但它不是我想要的形式。

import json
import pandas as pd

# Import the data
with open("2019_07_02_eco copy.json", "r", encoding="utf-8") as f:
    data_list = json.load(f)

# Create a pandas DataFrame with the follower ids 
df = pd.DataFrame(data_list)

print(df.head)

我期望的是一个 pd 数据框,其中帐户 ID(50 个帐户的)作为列标题,追随者 ID 位于其下面的行中。

我得到的是这样的:

[194 rows x 1 columns] 

<bound method NDFrame.head of                                                      0
0                                             36146779
1    [170742628, 3597763396, 247113642, 11306966070...
2                                             22971125
3    [1114702974, 1145981566365130758, 111840995856...
4    [295929695, 1024767030065606657, 1007033735013...
5    [984651561518252032, 982678444088541184, 98696...
6    [227843834, 23838268, 43140516, 2790255573, 33...
7                                            111125168
8    [1144607601914720258, 70032358, 18055487, 1127...
9    [947686805809266688, 9701692, 1096088766, 3337...
10   [2967527466, 2269464956, 249752699, 7556396244...
11   [321553655, 3546285436, 126038375, 71595951158...
12                                            71280747
13   [2955657113, 192354019, 1641657258, 375061682,...
14   [900344203955367937, 221726613, 1358476824, 14...
15   [2304150619, 14436400, 4833507964, 4883671481,...
16   [274049948, 2796219727, 185657334, 993542912, ...
17                                            72665016
18   [4892138044, 19982260, 3150202778, 73071487944...
19   [20389458, 386293346, 590031373, 576342755, 52...
20                                          1289611591
21   [3252647829, 16817453, 56003694, 1039493295318...
22                                            25088527
23   [436396700, 993251142263099392, 11435552424428...
24   [329428581, 20537025, 1724220128, 1682340361, ...
25   [15005765, 15678953, 54576200, 7521632, 121736...
26                                            19954039
27   [1033101308935462912, 1145323862969790464, 866...

最佳答案

试试这个:

data = [
    36146779,
    [
        170742628,
        3597763396,
        13453212,
        24763726,
        19087188,
        19605181,
        37374972
    ],
    22971125,
    [
        1114702974,
        1145981566365130758,
        1118409958561685504,
        822439041312423941,
        1110524937788424197,
        807718095460581376,
        24763726,
        3181477874,
        1076870147980300288,
        307465302,
    ],
    24763726,
    [
        1145981566365130758,
        1118409958561685504,
        822439041312423941,
        1110524937788424197,
        22971125
    ]
    ]

d = {}
for i in range(0,len(data)-1,2): # convert to dictionary
    d[str(data[i])] = data[i+1]

def getKeys(dictOfElements, valueToFind):
    listOfKeys = list()
    listOfItems = dictOfElements.items()
    for item  in listOfItems:
        if valueToFind in item[1]:
            listOfKeys.append(item[0])
    return  listOfKeys


for key in d.keys():
    keys = ",".join(getKeys(d, int(key)))
    print ("person: {}, follows accounts: {}".format(key, keys))

输出:

person: 36146779, follows accounts: 
person: 22971125, follows accounts: 24763726
person: 24763726, follows accounts: 36146779,22971125

关于python - 如何将具有多个不同长度值的嵌套列表转换为python中的pandas数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56931274/

相关文章:

python - Django - 模板上下文处理器 - 破坏我的应用程序

python - 将数字追加到列表的指定元素中

python - 使用术语和返回键搜索字典条件

python - 根据python中其他列表的值获取最大子列表

python - 将文件中的不同列附加到不同的列表?

python - 迭代字典中字典的键内的值

c# - C# 字典是如何实现的? (C++)

python - 检查Python中分隔符后面的字符是否存在

Python 3.3 - Unicode 对象必须在散列之前进行编码

python - 一个词在一个文件中出现了多少次?