How to append the list in a dictionary into its own column?

343 Views Asked by At

I have a dictionary with lists as values as you can see here:

my_dict  = {1: ['Home', 'Stories'], 2: ['Sounds', 'Stories', 'Home', 'Home', 'Stories'], 3: ['Journeys / Series', 'Journeys / Series', 'Journeys / Series'], 4: [ 'Home', 'Home', 'Home'], 5: ['Home', 'Stories', 'Home', 'Home', 'Home', 'Cancellation Flow'], 6: ['My Account', 'My Account']}

I would like to turn the lists within these dictionaries into string and append them into their own column.

This is what I tried so far, but it does not work as the values in the dictionary are lists.

keys_list = list(my_dict.keys())
values_list = list(my_dict.values())

df_keyvalues = pd.DataFrame({'Column1': keys_list, 'Column2': values_list})

df_keyvalues

I would like my output to look like this:

User       List
1          'Home', 'Stories'
2          'Sounds', 'Stories', 'Home', 'Home', 'Stories'
3          'Journeys / Series', 'Journeys / Series', 'Journeys / Series'
4          'Home', 'Home', 'Home'
5          'Home', 'Stories', 'Home', 'Home', 'Home', 'Cancellation Flow'
6          'My Account', 'My Account'
4

There are 4 best solutions below

0
lotus On

With delimiter.join(list) you can join your lists as a string:

keys_list = list(my_dict.keys())
values_list = [", ".join(i) for i in list(my_dict.values())]
df_keyvalues = pd.DataFrame({'User': keys_list, 'List': values_list})

Is this what your are looking for?

0
SIGHUP On

You don't need a Dataframe in order to produce that output

my_dict = {
    1: ['Home', 'Stories'],
    2: ['Sounds', 'Stories', 'Home', 'Home', 'Stories'],
    3: ['Journeys / Series', 'Journeys / Series', 'Journeys / Series'],
    4: ['Home', 'Home', 'Home'],
    5: ['Home', 'Stories', 'Home', 'Home', 'Home', 'Cancellation Flow'],
    6: ['My Account', 'My Account']
}

PAD = 10

def quote(s):
    return f"'{s}'"

print('{:<{}}List'.format('User', PAD))

for k, v in my_dict.items():
    print(f'{k:<{PAD}}{", ".join(map(quote, v))}')

Output:

User      List
1         'Home', 'Stories'
2         'Sounds', 'Stories', 'Home', 'Home', 'Stories'
3         'Journeys / Series', 'Journeys / Series', 'Journeys / Series'
4         'Home', 'Home', 'Home'
5         'Home', 'Stories', 'Home', 'Home', 'Home', 'Cancellation Flow'
6         'My Account', 'My Account'
0
Rafi Patel On

Try

keys_list = list(my_dict.keys())
values_list = list(my_dict.values())

df_keyvalues = pd.DataFrame({'Column1': keys_list, 'Column2': values_list})

#This will convert list to a string
df_keyvalues['Column2'] = df_keyvalues['Column2'].apply(lambda x: ','.join(x))

df_keyvalues

but yes it will not be "Home","Stories" it will be Home,Stories, i hope that will not be an issue.

0
Sekhar On

If your intension is to convert the strings in list to a single string(in which the strings in list will be separated by comma in result string), the following is the solution. no need to explicitly mention list() when using dict.keys() and dict.values() as both keys() and values() functions return lists only.

keys_list = my_dict.keys()
values_list = my_dict.values()
new_list=[",".join(i) for i in values_list]
df_keyvalues = pd.DataFrame({'User': keys_list, 'List': new_list})

print(df_keyvalues.to_string(index=False))

output would be single string but not strings separated by commas. Output