从字典创建一个包含一行数据的DataFrame,其中一个值是一个列表。

huangapple go评论98阅读模式
英文:

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 :
以下是一个非常简单的示例:

  1. >>> import pandas as pd
  2. >>> my_list = [1,2,3]
  3. >>> year = 2023
  4. >>> item = 'obj'
  5. >>> res_dict = {"year" : year, "values" : my_list, "name" : item }
  6. >>> print(res_dict)
  7. {'year': 2023, 'values': [1, 2, 3], 'name': 'obj'}
  8. >>> df = pd.DataFrame(res_dict)
  9. >>> print(df)
  10. year values name
  11. 0 2023 1 obj
  12. 1 2023 2 obj
  13. 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 :
所以类似这样:

  1. year values name
  2. 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,其中包含我列表中的每个元素的列?以获得类似以下的结果:

  1. year values1 values2 values3 name
  2. 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 :

  1. >>> import pandas as pd
  2. >>> my_list = [1,2,3]
  3. >>> year = 2023
  4. >>> item = 'obj'
  5. >>> res_dict = {"year" : year, "values" : my_list, "name" : item }
  6. >>> print(res_dict)
  7. {'year': 2023, 'values': [1, 2, 3], 'name': 'obj'}
  8. >>> df = pd.DataFrame(res_dict)
  9. >>> print(df)
  10. year values name
  11. 0 2023 1 obj
  12. 1 2023 2 obj
  13. 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 :

  1. year values name
  2. 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 :

  1. year values1 values2 values3 name
  2. 0 2023 1 2 3 obj

答案1

得分: 3

以下是您要翻译的代码部分:

  1. import pandas as pd
  2. my_list = [1, 2, 3]
  3. year = 2023
  4. item = 'obj'
  5. res_dict = {"year": year, "name": item}
  6. for i, value in enumerate(my_list):
  7. column_name = f"value{i+1}"
  8. res_dict[column_name] = value
  9. df = pd.DataFrame(res_dict, index=[0])
  10. print(df)
  1. year name value1 value2 value3
  2. 0 2023 obj 1 2 3
英文:

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

  1. import pandas as pd
  2. my_list = [1, 2, 3]
  3. year = 2023
  4. item = 'obj'
  5. res_dict = {"year": year, "name": item}
  6. for i, value in enumerate(my_list):
  7. column_name = f"value{i+1}"
  8. res_dict[column_name] = value
  9. df = pd.DataFrame(res_dict, index=[0])
  10. print(df)
  1. year name value1 value2 value3
  2. 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

这是你的第一个问题 - 只需将你的列表包含在另一个列表中,这样当它遍历值时,它会得到一个列表。

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

这是你的第二个问题,将值扩展到你的数据字典中 - 只需记住传入一个索引,因为DataFrame只有一行。

  1. import pandas as pd
  2. my_list = [1,2,3]
  3. year = 2023
  4. item = 'obj'
  5. res_dict = {"year" : year, **{"values" + str(n+1):item for n,item in enumerate(my_list)}, "name" : item}
  6. print(res_dict)
  7. df = pd.DataFrame(res_dict,index=[0])
  8. print(df)
  1. year values1 values2 values3 name
  2. 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.

  1. import pandas as pd
  2. my_list = [1,2,3]
  3. year = 2023
  4. item = 'obj'
  5. res_dict = {"year" : year, "values" : [my_list], "name" : item}
  6. print(res_dict)
  7. df = pd.DataFrame(res_dict)
  8. print(df)
  1. year values name
  2. 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.

  1. import pandas as pd
  2. my_list = [1,2,3]
  3. year = 2023
  4. item = 'obj'
  5. res_dict = {"year" : year, **{"values" + str(n+1):item for n,item in enumerate(my_list)}, "name" : item}
  6. print(res_dict)
  7. df = pd.DataFrame(res_dict,index=[0])
  8. print(df)
  1. year values1 values2 values3 name
  2. 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).

huangapple
  • 本文由 发表于 2023年6月4日 23:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定