【问题标题】:Convert JSON data to Pandas DataFrame [closed]将 JSON 数据转换为 Pandas DataFrame [关闭]
【发布时间】:2020-12-21 22:54:43
【问题描述】:
"{\".travis.yml\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/client-mac/com.google.code.fleetspeak.plist\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/client/client.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/client_test.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"Ben Galehouse\", \"mol123\"]}, \"count\": 2}, \"fleetspeak/src/client/entry/entry_unix.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/entry/entry_windows.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/entry/wait_unix.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/entry/wait_windows.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"fleetspeak/src/client/services.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"Ben Galehouse\", \"mol123\"]}, \"count\": 2}, \"fleetspeak/src/client/socketservice/checks/sock_checks_windows.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"Ben Galehouse\", \"Brendan Jackman\"]}, \"count\": 5}, \"fleetspeak/src/e2etesting/README.md\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/e2etesting/balancer/balancer.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 2}, \"fleetspeak/src/e2etesting/e2etest.sh\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 2}, \"fleetspeak/src/e2etesting/localtesting/end_to_end_test.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/e2etesting/run_end_to_end_tests.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 5}, \"fleetspeak/src/e2etesting/setup/setup_components.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"mol123\", \"Alexandr Tsaplin\"]}, \"count\": 7}, \"fleetspeak/src/e2etesting/tests/end_to_end_tests.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"mol123\", \"Alexandr Tsaplin\"]}, \"count\": 5}, \"fleetspeak/src/inttesting/frr/frr.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/inttesting/frr/proto/fleetspeak_frr/frr.pb.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/inttesting/frr/proto/fleetspeak_frr/frr.proto\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/inttesting/integrationtest/frr.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"tsehori\"]}, \"count\": 1}, \"fleetspeak/src/server/comms.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"Ben Galehouse\", \"mol123\"]}, \"count\": 4}, \"fleetspeak/src/server/components/components.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/server/components/prometheus/prometheus.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"tsehori\"]}, \"count\": 1}, \"fleetspeak/src/server/components/proto/fleetspeak_components/config.pb.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/server/components/proto/fleetspeak_components/config.proto\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/server/internal/services/manager.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"tsehori\"]}, \"count\": 1}, \"fleetspeak/src/server/server.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"fleetspeak/src/server/servertests/comms_test.go\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"Ben Galehouse\", \"mol123\"]}, \"count\": 4}, \"fleetspeak/src/server/stats.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"tsehori\"]}, \"count\": 1}, \"fleetspeak/src/server/stats/collector.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"tsehori\"]}, \"count\": 1}, \"fleetspeak/test.sh\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"frr_python/frr_server.py\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"frr_python/setup.py\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"go.mod\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"mol123\"]}, \"count\": 1}, \"go.sum\": {\"auth_count\": 2, \"authors\": {\"py/set\": [\"Ben Galehouse\", \"mol123\"]}, \"count\": 3}, \"terraform/README.md\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 2}, \"terraform/cloudtesting/end_to_end_test.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"terraform/fleetspeak_configurator/build_configs.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 3}, \"terraform/fs_client_start.sh\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 2}, \"terraform/fs_server_start.sh\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"terraform/main.tf\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 3}, \"terraform/main_vm_start.sh\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 4}, \"terraform/master_server_start.sh\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 1}, \"terraform/test_runner/run_tests.go\": {\"auth_count\": 1, \"authors\": {\"py/set\": [\"Alexandr Tsaplin\"]}, \"count\": 3}}"

大家好,

我想将 JSON 数据(存储在 result.json 文件中)转换为数据帧。

我希望我的数据框看起来像所附图片中的那样。

谢谢大家:)

【问题讨论】:

    标签: python json pandas dataframe


    【解决方案1】:

    您可以将 JSON 读入数据框,然后将作者从列表转换为字符串:

    # read json from string `s` to a dataframe
    df = (pd.read_json(s).T
        .reset_index()
        .rename(columns={'index': 'Filename', 'auth_count': 'authors_count'}))
    
    # convert lists of authors to comma-separated strings
    df['authors'] = df['authors'].apply(lambda x: ', '.join(x['py/set']))
    
    df
    

    输出:

                   Filename authors_count              authors count
    0           .travis.yml             1     Alexandr Tsaplin     1
    1   fleetspeak/clien...             1               mol123     1
    2   fleetspeak/src/c...             1               mol123     1
    3   fleetspeak/src/c...             2  Ben Galehouse, m...     2
    4   fleetspeak/src/c...             1               mol123     1
    5   fleetspeak/src/c...             1               mol123     1
    6   fleetspeak/src/c...             1               mol123     1
    

    【讨论】:

    • 非常感谢:) 效果很好。
    猜你喜欢
    • 2023-03-31
    • 2017-07-20
    • 2021-05-17
    • 2017-04-13
    • 2021-03-29
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多