Theresa S
Theresa S

Reputation: 37

How to turn a nested list with multiple values of different length into a pandas dataframe in python?

I am collecting follower ids (between 1,000 and 25,000 per account) from 50 twitter accounts and was able to store these follower ids in a json in a format similar to this:


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

What I am trying to do is to find all follower ids that are the same, say a person 24763726 both follows accounts 36146779 and 22971125. Any recommendations on how to solve this problem? I am quite new to Python and programming in general and would be very thankful about any help or advice!

So far I was able to turn the saved data (in json format) into a pandas dataframe, but it is not in the form I would like to have it.

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)

What I expected was a pd dataframe with account ids (of the 50 accounts) as column headers and follower ids in the lines below that.

What I got was this:

[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...

Upvotes: 1

Views: 212

Answers (2)

ncica
ncica

Reputation: 7206

try this:

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))

output:

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

Upvotes: 1

Olivier CAYROL
Olivier CAYROL

Reputation: 266

Your data is a single column that contains the id of the user and then the list of connected ids, and so on. It is translated into a DataFrame with a single column containing an integer or a list of integers.

Try grouping your elements two by two and then turning this list of two-elements lists into a dictionary before converting it in a DataFrame.

Upvotes: 0

Related Questions