将数据框按组存储为JSON格式

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

Store dataframe into json fromat with group by

问题

我正在寻找的输出如下所示:

id type
1 {"test":{"spark":2,"kafka":1},"test1":{"spark":2}}
2 {"test2":{"kafka":1}}
3 {"test":{"spark":2}}
英文:

I have DF in below format

id type application number
1 test spark 2
1 test kafka 1
1 test1 spark 2
2 test2 kafka 1
2 test2 kafka 1
3 test spark 2

o/p I am looking for is

id type
1 {"test":{"spark":2,"kafka":1},"test1":{"spark":2}}
2 {"test2":{"kafka":1}}
3 {"test":{"spark":2}}

I have tried several approaches but nothing returned me expected format

答案1

得分: 1

以下是翻译好的部分:

  1. 如果您的输入数据框不太大您可以按照以下代码依次循环每个唯一的id然后类型然后应用程序
  2. import pandas as pd
  3. df = pd.DataFrame(data=[
  4. [1, "test", "spark", 2],
  5. [1, "test", "kafka", 1],
  6. [1, "test1", "spark", 2],
  7. [2, "test2", "kafka", 1],
  8. [2, "test2", "kafka", 1],
  9. [3, "test", "spark", 2],
  10. ], columns=['id', 'type', 'application', 'number'])
  11. data = []
  12. for id in df['id'].unique():
  13. id = int(id)
  14. subdf1 = df[df['id']==id]
  15. row = {"id": id, "type": {}}
  16. for type in subdf1['type'].unique():
  17. subdf2 = subdf1[subdf1['type']==type]
  18. row["type"][type] = {}
  19. for application in subdf2['application'].unique():
  20. subdf3 = subdf2[subdf2['application']==application]
  21. # 取第一行的“number”
  22. # 在此之前,请确保所有具有相同id、类型、应用程序的输入行具有相同的“number”
  23. first_row = subdf3.iloc[0]
  24. number = int(first_row['number'])
  25. row["type"][type][application] = number
  26. data.append(row)
  27. out = pd.DataFrame(data)
  28. print(out)

输出:

  1. id type
  2. 0 1 {'test': {'spark': 2, 'kafka': 1}, 'test1': {'...
  3. 1 2 {'test2': {'kafka': 1}}
  4. 2 3 {'test': {'spark': 2}}
英文:

If your input dataframe is not too large, you can loop every unique id, then type, then application in turn as following code:

  1. import pandas as pd
  2. df = pd.DataFrame(data=[
  3. [1, "test", "spark", 2],
  4. [1, "test", "kafka", 1],
  5. [1, "test1", "spark", 2],
  6. [2, "test2", "kafka", 1],
  7. [2, "test2", "kafka", 1],
  8. [3, "test", "spark", 2],
  9. ], columns=['id', 'type', 'application', 'number'])
  10. data = []
  11. for id in df['id'].unique():
  12. id = int(id)
  13. subdf1 = df[df['id']==id]
  14. row = {"id": id, "type": {}}
  15. for type in subdf1['type'].unique():
  16. subdf2 = subdf1[subdf1['type']==type]
  17. row["type"][type] = {}
  18. for application in subdf2['application'].unique():
  19. subdf3 = subdf2[subdf2['application']==application]
  20. # Take the "number" of first row
  21. # before that, make sure all input rows with same id,type,application have same "number",
  22. first_row = subdf3.iloc[0]
  23. number = int(first_row['number'])
  24. row["type"][type][application] = number
  25. data.append(row)
  26. out = pd.DataFrame(data)
  27. print(out)

Output:

  1. id type
  2. 0 1 {'test': {'spark': 2, 'kafka': 1}, 'test1': {'...
  3. 1 2 {'test2': {'kafka': 1}}
  4. 2 3 {'test': {'spark': 2}}

答案2

得分: 0

I was not able to get exact what I was looking for but able to reach nearby solution

  1. labelled_df = labelled_df[['id', 'type', 'application', 'number']].drop_duplicates()
  2. group_df = labelled_df.groupby(['id', 'type'])[['application', 'number']].apply(
  3. lambda g: [{row[0]: row[1]} for row in g.values.tolist()]).reset_index(name='group1').groupby('id')[
  4. 'type', 'group1'].apply(lambda g: [{row[0]: row[1]} for row in g.values.tolist()]).reset_index(
  5. name='applications')

which resulted in

  1. asset_id applications
  2. 0 1 [{'test': [{'spark': 2}, {'kafka': 1}]}, {'test1': [{'spark': 2}]}]
  3. 1 2 [{'test2': [{'kafka': 1}]}]
  4. 2 3 [{'test': [{'spark': 2}]}]
英文:

I was not able to get exact what I was looking for but able to reach nearby solution

  1. labelled_df = labelled_df[["id", "type", "application", "number"]].drop_duplicates()
  2. group_df = labelled_df.groupby(['id', 'type'])[['application', 'number']].apply(
  3. lambda g: [{row[0]: row[1]} for row in g.values.tolist()]).reset_index(name='group1').groupby('id')[
  4. 'type', 'group1'].apply(lambda g: [{row[0]: row[1]} for row in g.values.tolist()]).reset_index(
  5. name='applications')

which resulted in

  1. asset_id applications
  2. 0 1 [{'test': [{'spark': 2}, {'kafka': 1}]}, {'test1': [{'spark': 2}]}]
  3. 1 2 [{'test2': [{'kafka': 1}]}]
  4. 2 3 [{'test': [{'spark': 2}]}]
  5. </details>

huangapple
  • 本文由 发表于 2023年7月6日 18:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627849.html
匿名

发表评论

匿名网友

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

确定