1

A little problem that leads me to two (or even 3) questions. And I have some difficulties to google the answer.

Here the very simple example :

>>> import pandas as pd
>>> my_list = [1,2,3]
>>> year = 2023
>>> item = 'obj'
>>> res_dict = {"year" : year, "values" : my_list, "name" : item }
>>> print(res_dict)
{'year': 2023, 'values': [1, 2, 3], 'name': 'obj'}
>>> df = pd.DataFrame(res_dict)
>>> print(df)
   year  values name
0  2023       1  obj
1  2023       2  obj
2  2023       3  obj

My problem that I want a slightly different DataFrame.

My first idea was to create a DataFrame where in values we store a list. So something like this :

   year     values  name
0  2023  [1, 2, 3]   obj

But, and here is the first question : it seems that while it's possible to put list into cell of DataFrame it"s not really a good idea. If it's so...

The second question. How could I instead create a DataFrame with columns for each element of my list ? To get something like this :

   year  values1  values2  values3  name
0  2023        1        2        3   obj

2 Answers 2

3

Here's your first question - just enclose your list in another list, so when it iterates through the values it gets the one list.

import pandas as pd
my_list = [1,2,3]
year = 2023
item = 'obj'
res_dict = {"year" : year, "values" : [my_list], "name" : item}
print(res_dict)
df = pd.DataFrame(res_dict)
print(df)
   year     values name
0  2023  [1, 2, 3]  obj

And here's your second, expanding values into your data dictionary - just remember to pass in an index, as the df only has one row.

import pandas as pd
my_list = [1,2,3]
year = 2023
item = 'obj'
res_dict = {"year" : year, **{"values" + str(n+1):item for n,item in enumerate(my_list)}, "name" : item}
print(res_dict)
df = pd.DataFrame(res_dict,index=[0])
print(df)
   year  values1  values2  values3 name
0  2023        1        2        3  obj

Both of these solutions would strike me as a little weird if I saw them in live code, pandas is designed to be rather like a spreadsheet, so having lists in data cells is awkward and not natural, but you can definitely do it. The second solution isn't really well normalized (https://en.wikipedia.org/wiki/Database_normalization).

3
  • About discussion of weird solutions. I'd like to have some advice. In fact I have some data for each year. Almost all of data is just a numbers, but one of them is a list. For example a number of sales in each quarter of the year ([1, 2, 3, 4] : I have one sale in first 3 months, 2 sales in second quarter etc). As all others data is just the numbers it seems strange to create multiples rows. So I think about these two solutions. Insert a list or convert this list to separate columns. Do you have a better idea ? Jun 4 at 16:10
  • 1
    If I were working on stores and a list of their sales, I probably wouldn't use pandas to store the data, but rather native python classes - ie, a store class with a list of sales. Pandas is very good for exporting data for user review, though.
    – Carbon
    Jun 4 at 16:13
  • 1
    it's not a software. Globally it's just to get some data, do some calculations and then plot the graphs. So the choice of DataFrames seemed like a good idea for me. Jun 4 at 16:18
3

In answer to your second question, you could try something like this:

import pandas as pd

my_list = [1, 2, 3]
year = 2023
item = 'obj'

res_dict = {"year": year, "name": item}

for i, value in enumerate(my_list):
    column_name = f"value{i+1}"
    res_dict[column_name] = value

df = pd.DataFrame(res_dict, index=[0])
print(df)
   year name  value1  value2  value3
0  2023  obj       1       2       3

In answer to your first question, it depends what you need to do with that data and how you want to access it once it's in the df.

2
  • 1
    by 21 seconds! well done
    – Carbon
    Jun 4 at 15:54
  • @doine : Will you be upset is I accept Carbon answers? You answer is perfectly correct, but I found Carbon answer a little bit more pythonic and a little bit more detailed. Jun 4 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.