“ValueError: cannot set a frame with no defined columns” 在追加两个数据框时出现。

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

While appending 2 dataframes getting "ValueError: cannot set a frame with no defined columns"

问题

数据帧1位于一个函数内,数据帧2位于主函数中,当我运行主函数时,它从JSON中附加值并存储在result_df中,出现ValueError: cannot set a frame with no defined columns错误。

为什么我要在主函数中创建数据帧2?
我在其他函数中使用数据帧2(total_device_df)将其转换为CSV。

可重现的代码:

  1. import pandas as pd
  2. import os
  3. import json
  4. class KatsRequest:
  5. currDir = os.getcwd()
  6. def parse_json_response():
  7. filename = "my_json_file.json"
  8. device_name = ["Trona", "Sheldon"]
  9. "创建数据帧以存储结果"
  10. column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
  11. result_df = pd.DataFrame(columns=column_names)
  12. my_json_file = currDir + '/' + filename
  13. for i in range(len(device_name)):
  14. my_device_name = device_name[i]
  15. with open(my_json_file) as f:
  16. data = json.load(f)
  17. for devices in data:
  18. device_types = devices['device_types']
  19. if my_device_name in device_types['name']:
  20. if device_types['name'] == my_device_name:
  21. device = devices['device_types']['name']
  22. last_updated = devices['devices']['last_status_update']
  23. device_status = devices['devices']['status']
  24. result_df.loc[len(result_df)] = {'DEVICE': device, 'STATUS': device_status, 'LAST UPDATED': last_updated}
  25. return result_df
  26. def main():
  27. total_device_df = pd.DataFrame()
  28. total_device_df.loc[len(total_device_df)] = KatsRequest().parse_json_response()
  29. if __name__ == '__main__':
  30. main()

这是我的JSON文件内容:(保存在当前路径中,命名为"my_json_file.json")

  1. [{"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Trona"}}, {"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Sheldon"}}]

输出:
ValueError: cannot set a frame with no defined columns

这里缺少/错误了什么?

英文:

Dataframe 1 is inside a function and Dataframe 2 is in main function when I run main function, it appends the values from a JSON and stores in result_df and getting ValueError: cannot set a frame with no defined columns error

Why I am creating Dataframe2 in main function?
I am using Dataframe2 (total_device_df) in other functions to convert to csv.

Reproducible code:

  1. import pandas as pd
  2. import os
  3. import json
  4. class KatsRequest:
  5. currDir = os.getcwd()
  6. def parse_json_response():
  7. filename = "my_json_file.json"
  8. device_name = ["Trona", "Sheldon"]
  9. "creating dataframe to store result"
  10. column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
  11. result_df = pd.DataFrame(columns=column_names)
  12. my_json_file = currDir + '/' + filename
  13. for i in range(len(device_name)):
  14. my_device_name = device_name[i]
  15. with open(my_json_file) as f:
  16. data = json.load(f)
  17. for devices in data:
  18. device_types = devices['device_types']
  19. if my_device_name in device_types['name']:
  20. if device_types['name'] == my_device_name:
  21. device = devices['device_types']['name']
  22. last_updated = devices['devices']['last_status_update']
  23. device_status = devices['devices']['status']
  24. result_df.loc[len(result_df)] = {'DEVICE': device, 'STATUS': device_status, 'LAST UPDATED': last_updated}
  25. return result_df
  26. def main()
  27. total_device_df = pd.DataFrame()
  28. total_device_df.loc[len(total_device_df)] = KatsRequest().parse_json_response(filename, device_names)
  29. if __name__ == '__main__':
  30. main()

Here is my JSON file contents: (save in your current path named as "my_json_file.json")

  1. [{"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Trona"}}, {"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Sheldon"}}]

Output:
ValueError: cannot set a frame with no defined columns

What is missing/wrong here?

答案1

得分: 1

(你的示例无法重现)

如果你想要合并 total_device_dfparse_json_response 返回的数据框,你需要使用 pd.concat

  1. def main():
  2. total_device_df = pd.DataFrame()
  3. total_device_df = pd.concat([total_device_df, KatsRequest().parse_json_response(filename, device_names)])

或者简化为:

  1. def main():
  2. total_device_df = KatsRequest().parse_json_response(filename, device_names)

完整示例:

  1. import os
  2. import json
  3. import pandas as pd
  4. currDir = os.getcwd()
  5. filename = 'my_json_file.json'
  6. device_names = ['Trona', 'Sheldon']
  7. class KatsRequest:
  8. def parse_json_response(self, filename, device_name):
  9. # 创建用于存储结果的数据框
  10. column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
  11. result_df = pd.DataFrame(columns=column_names)
  12. my_json_file = currDir + '/' + filename
  13. for i in range(len(device_name)):
  14. my_device_name = device_name[i]
  15. with open(my_json_file) as f:
  16. data = json.load(f)
  17. for devices in data:
  18. device_types = devices['device_types']
  19. if my_device_name in device_types['name']:
  20. if device_types['name'] == my_device_name:
  21. device = devices['device_types']['name']
  22. last_updated = devices['devices']['last_status_update']
  23. device_status = devices['devices']['status']
  24. result_df.loc[len(result_df)] = {'DEVICE': device, 'STATUS': device_status, 'LAST UPDATED': last_updated}
  25. return result_df
  26. def main():
  27. total_device_df = pd.DataFrame()
  28. total_device_df = pd.concat([total_device_df, KatsRequest().parse_json_response(filename, device_names)])
  29. print(total_device_df)
  30. if __name__ == '__main__':
  31. main()

输出:

  1. >>> total_device_df
  2. DEVICE STATUS LAST UPDATED
  3. 0 Trona idle 2023-05-25 07:56:49
  4. 1 Sheldon idle 2023-05-25 07:56:49

更新

一种简便的方法是使用 pd.json_normalize

  1. with open('my_json_file.json') as jp:
  2. data = json.load(jp)
  3. total_device_df = (pd.json_normalize(data).loc[lambda x: x['device_types.name'].isin(device_names)]
  4. .rename(columns=lambda x: x.split('.', maxsplit=1)[-1].upper()))

输出:

  1. >>> total_device_df
  2. ID LAST_STATUS_UPDATE STATUS NAME
  3. 0 34815 2023-05-25 07:56:49 idle Trona
  4. 1 34815 2023-05-25 07:56:49 idle Sheldon

如何在完整示例代码中使用 pd.json_normalize 方法?

  1. import os
  2. import json
  3. import pandas as pd
  4. currDir = os.getcwd()
  5. filename = 'my_json_file.json'
  6. device_names = ['Trona', 'Sheldon']
  7. class KatsRequest:
  8. def parse_json_response(self, filename, device_name):
  9. # 创建用于存储结果的数据框
  10. dmap = {
  11. 'device_types.name': 'DEVICE',
  12. 'devices.status': 'STATUS',
  13. 'devices.last_status_update': 'LAST STATUS'
  14. }
  15. my_json_file = currDir + '/' + filename
  16. with open(my_json_file) as f:
  17. data = json.load(f)
  18. results_df = (pd.json_normalize(data)[dmap.keys()].rename(columns=dmap)
  19. .loc[lambda x: x['DEVICE'].isin(device_names)])
  20. return results_df
  21. def main():
  22. total_device_df = pd.DataFrame()
  23. total_device_df = pd.concat([total_device_df, KatsRequest().parse_json_response(filename, device_names)])
  24. print(total_device_df)
  25. if __name__ == '__main__':
  26. main()

希望这些翻译对你有帮助。

英文:

(Your example is not reproducible)

You have to use pd.concat if you want to use merge total_device_df and the dataframe returned by parse_json_response.

  1. def main():
  2. total_device_df = pd.DataFrame()
  3. total_device_df = pd.concat([total_device_df, KatsRequest().parse_json_response(filename, device_names)])

Or simply:

  1. def main():
  2. total_device_df = pd.DataFrame()
  3. total_device_df = KatsRequest().parse_json_response(filename, device_names)

Full example:

  1. import os
  2. import json
  3. import pandas as pd
  4. currDir = os.getcwd()
  5. filename = 'my_json_file.json'
  6. device_names = ['Trona', 'Sheldon']
  7. class KatsRequest:
  8. def parse_json_response(self, filename, device_name):
  9. # creating dataframe to store result
  10. column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
  11. result_df = pd.DataFrame(columns=column_names)
  12. my_json_file = currDir + '/' + filename
  13. for i in range(len(device_name)):
  14. my_device_name = device_name[i]
  15. with open(my_json_file) as f:
  16. data = json.load(f)
  17. for devices in data:
  18. device_types = devices['device_types']
  19. if my_device_name in device_types['name']:
  20. if device_types['name'] == my_device_name:
  21. device = devices['device_types']['name']
  22. last_updated = devices['devices']['last_status_update']
  23. device_status = devices['devices']['status']
  24. result_df.loc[len(result_df)] = {'DEVICE': device, 'STATUS': device_status, 'LAST UPDATED': last_updated}
  25. return result_df
  26. def main():
  27. total_device_df = pd.DataFrame()
  28. total_device_df = pd.concat([total_device_df, KatsRequest().parse_json_response(filename, device_names)])
  29. print(total_device_df)
  30. if __name__ == '__main__':
  31. main()

Output:

  1. >>> total_device_df
  2. DEVICE STATUS LAST UPDATED
  3. 0 Trona idle 2023-05-25 07:56:49
  4. 1 Sheldon idle 2023-05-25 07:56:49

Update:

A painless way is to use pd.json_normalize:

  1. with open('my_json_file.json') as jp:
  2. data = json.load(jp)
  3. total_device_df = (pd.json_normalize(data).loc[lambda x: x['device_types.name'].isin(device_names)]
  4. .rename(columns=lambda x: x.split('.', maxsplit=1)[-1].upper()))

Output:

  1. >>> total_device_df
  2. ID LAST_STATUS_UPDATE STATUS NAME
  3. 0 34815 2023-05-25 07:56:49 idle Trona
  4. 1 34815 2023-05-25 07:56:49 idle Sheldon

> How to use pd.json_normalize method in full example code?

  1. import os
  2. import json
  3. import pandas as pd
  4. currDir = os.getcwd()
  5. filename = 'my_json_file.json'
  6. device_names = ['Trona', 'Sheldon']
  7. class KatsRequest:
  8. def parse_json_response(self, filename, device_name):
  9. # creating dataframe to store result
  10. dmap = {
  11. 'device_types.name': 'DEVICE',
  12. 'devices.status': 'STATUS',
  13. 'devices.last_status_update': 'LAST STATUS'
  14. }
  15. my_json_file = currDir + '/' + filename
  16. with open(my_json_file) as f:
  17. data = json.load(f)
  18. results_df = (pd.json_normalize(data)[dmap.keys()].rename(columns=dmap)
  19. .loc[lambda x: x['DEVICE'].isin(device_names)])
  20. return results_df
  21. def main():
  22. total_device_df = pd.DataFrame()
  23. total_device_df = pd.concat([total_device_df, KatsRequest().parse_json_response(filename, device_names)])
  24. print(total_device_df)
  25. if __name__ == '__main__':
  26. main()

huangapple
  • 本文由 发表于 2023年5月26日 14:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338060.html
匿名

发表评论

匿名网友

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

确定