如何在新列中提取pandas DataFrame的字典值?

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

How to extract dict values of pandas DataFrame in new columns?

问题

I would like to extract the values of a dictionary inside a Pandas DataFrame df into new columns of that DataFrame. All keys in the referring dict are the same across all rows.

  1. import pandas as pd
  2. df = pd.DataFrame({'a': [1, 2, 3], 'b': [{'x':[101], 'y': [102], 'z': [103]}, {'x':[201], 'y': [202], 'z': [203]}, {'x':[301], 'y': [302], 'z': [303]}]})

如何在新列中提取pandas DataFrame的字典值?

  1. dfResult = pd.DataFrame({'a': [1, 2, 3], 'x':[101, 201, 301], 'y': [102, 202, 302], 'z': [103, 203, 303]})

如何在新列中提取pandas DataFrame的字典值?

I am as far as I can get the keys and values out of the dict, but I do not know how to make new columns out of them:

  1. df.b.apply(lambda x: [x[y] for y in x.keys()])
  2. 0 [[101], [102], [103]]
  3. 1 [[201], [202], [203]]
  4. 2 [[301], [302], [303]]
  5. df.b.apply(lambda x: [y for y in x.keys()])
  6. 0 [x, y, z]
  7. 1 [x, y, z]
  8. 2 [x, y, z]
英文:

I would like to extract the values of a dictionary inside a Pandas DataFrame df into new columns of that DataFrame. All keys in the referring dict are the same across all rows.

  1. import pandas as pd
  2. df = pd.DataFrame({'a': [1, 2, 3], 'b': [{'x':[101], 'y': [102], 'z': [103]}, {'x':[201], 'y': [202], 'z': [203]}, {'x':[301], 'y': [302], 'z': [303]}]})

如何在新列中提取pandas DataFrame的字典值?

  1. dfResult = pd.DataFrame({'a': [1, 2, 3], 'x':[101, 201, 301], 'y': [102, 202, 302], 'z': [103, 203, 303]})

如何在新列中提取pandas DataFrame的字典值?

I am as far as I can get the keys and values out of the dict, but I do not know how to make new columns out of them:

  1. df.b.apply(lambda x: [x[y] for y in x.keys()])
  2. 0 [[101], [102], [103]]
  3. 1 [[201], [202], [203]]
  4. 2 [[301], [302], [303]]
  5. df.b.apply(lambda x: [y for y in x.keys()])
  6. 0 [x, y, z]
  7. 1 [x, y, z]
  8. 2 [x, y, z]

答案1

得分: 3

如果始终存在只有一个元素的列表,可以使用嵌套列表与字典推导,然后传递给 DataFrame 构造函数:

  1. df = df.join(pd.DataFrame([{k: v[0] for k, v in x.items()} for x in df.pop('b')],
  2. index=df.index))
  3. print (df)
  4. a x y z
  5. 0 1 101 102 103
  6. 1 2 201 202 203
  7. 2 3 301 302 303

另一种方法是为字典推导中的每一行创建一个 DataFrame,并使用 concat 进行连接:

  1. df = df.join(pd.concat({k: pd.DataFrame(v) for k, v in df.pop('b').items()}).droplevel(1))
  2. print (df)
  3. a x y z
  4. 0 1 101 102 103
  5. 1 2 201 202 203
  6. 2 3 301 302 303
英文:

If there are always one element lists is possible use nested list with dictionary comprehension and pass to DataFrame constructor:

  1. df = df.join(pd.DataFrame([{k: v[0] for k, v in x.items()} for x in df.pop('b')],
  2. index=df.index))
  3. print (df)
  4. a x y z
  5. 0 1 101 102 103
  6. 1 2 201 202 203
  7. 2 3 301 302 303

Another idea is create DataFrame for each row in dictionary comprehension and join by concat:

  1. df = df.join(pd.concat({k: pd.DataFrame(v) for k, v in df.pop('b').items()}).droplevel(1))
  2. print (df)
  3. a x y z
  4. 0 1 101 102 103
  5. 1 2 201 202 203
  6. 2 3 301 302 303

huangapple
  • 本文由 发表于 2023年5月17日 12:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268656.html
匿名

发表评论

匿名网友

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

确定