Home > Software design >  Creating a dataframe from two lists, one of them is nested
Creating a dataframe from two lists, one of them is nested

Time:12-29

Can someone advise me how to create a dataframe? It's a toy example, real one is much bigger. I have a user list

user_lst = ['user1', 'user2', 'user3']

And i have a list of lists

big_lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

how to make a dataframe and put inner lists as column values and repeat user names number of times equal to length of a small list? This example has too much manual work; I would like to iterate somehow through the lists and do it automatically.

b_lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']

B = [['user1', 'user1', 'user1', 'user2', 'user2', 'user2', 'user3', 'user3', 'user3'], b_lst]
df1 = pd.DataFrame()
df1['user'] = [B[0][i] for i in range(9)]
df1['value'] = [B[1][i] for i in range(9)]
df1
    user value
0  user1     a
1  user1     b
2  user1     c
3  user2     d
4  user2     e
5  user2     f
6  user3     g
7  user3     h
8  user3     i

CodePudding user response:

You can simply put the lists into the df then explode them.

df = pd.DataFrame({'user': user_lst, 'value': big_lst})
df = df.explode('value').reset_index(drop=True)
    user value
0  user1     a
1  user1     b
2  user1     c
3  user2     d
4  user2     e
5  user2     f
6  user3     g
7  user3     h
8  user3     i

CodePudding user response:

You can zip the user to its value list and create a row from each couple:

import pandas as pd

user_lst = ['user1', 'user2', 'user3']
big_lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
df = pd.DataFrame(
    [(user, value) for user, l in zip(user_lst, big_lst) for value in l],
    columns=["user", "value"])
print(df)
  • Related