英文:
Create one row DataFrame from dict, where one of value is a list
问题
以下是您提供的内容的翻译:
A little problem that leads me to two (or even 3) questions. And I have some difficulties to google the answer.
一个小问题引发了我两个(甚至是3个)问题。我在搜索答案时遇到了一些困难。
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.
我的问题是,我想要一个稍微不同的DataFrame。
My first idea was to create a DataFrame where in values we store a list.
所以我的第一个想法是创建一个DataFrame,在"values"列中存储一个列表。
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.
但是,这里是第一个问题:似乎虽然可以将列表放入DataFrame的单元格中,但这并不是一个好主意。
The second question. How could I instead create a DataFrame with columns for each element of my list ? To get something like this :
第二个问题是,我如何创建一个DataFrame,其中包含我列表中的每个元素的列?以获得类似以下的结果:
year values1 values2 values3 name
0 2023 1 2 3 obj
这就是您提出的问题的翻译。如果您有任何其他需要,请随时提出。
英文:
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
答案1
得分: 3
以下是您要翻译的代码部分:
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 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
得分: 3
这是你的第一个问题 - 只需将你的列表包含在另一个列表中,这样当它遍历值时,它会得到一个列表。
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
这是你的第二个问题,将值扩展到你的数据字典中 - 只需记住传入一个索引,因为DataFrame只有一行。
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
这两种解决方案都在实际代码中看起来有点奇怪,因为pandas的设计类似于电子表格,因此在数据单元格中包含列表会显得不太自然,但你确实可以这样做。第二个解决方案并没有很好地进行规范化(https://en.wikipedia.org/wiki/Database_normalization)。
英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论