简体   繁体   中英

List of lists to a Pandas dataframe

Hi I am trying to make a look up list, that given a listID I can find the users who have it, and given a UserID I can find all lists of that user.

The data comes in this format:

[['34', '345'],
['12', '23,534,34'],
['1', '13,42']]

What I would like is a pandas dataframe that looks like:

UserID, ListID
34, 345
12, 23
12, 534
12, 34
 1, 13
 1, 42

My thoughts were to make the second string to a list splitting on 'commas', but from there I am stuck. Any suggestions?

You should clean up your data before feeding it into the data frame constructor. Here is a simple script:

import pandas as pd

data = [['34', '345'],
['12', '23,534,34'],
['1', '13,42']]

new_data = []
for row in data:
    x, yvals = row
    for y in yvals.split(','):
        new_data.append([x,y])

df = pd.DataFrame(new_data, columns=['UserID', 'ListID'])

Here's one way

In [386]: L = [['34', '345'], ['12', '23,534,34'], ['1', '13,42']]

In [387]: (pd.DataFrame(L, columns=['UserID', 'ListID'])
             .set_index('UserID')
             .ListID.str.split(',')
             .apply(pd.Series)
             .stack()
             .reset_index(level=0, name='ListID'))
Out[387]:
  UserID ListID
0     34    345
1     12     23
2     12    534
3     12     34
4      1     13
5      1     42

You can do as follow :

df_tmp = pd.DataFrame([['34', '345'],
['12', '23,534,34'],
['1', '13,42']], columns=['ListID', 'UserIDs'])

s = df_tmp['UserIDs'].str.split(',', expand=True).stack()
i = s.index.get_level_values(0)
df = df_tmp.loc[i].copy()
df["UserID"] = s.values
del df['UserIDs']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM