【问题标题】:Cannot import the whole JSON file into Google Colab无法将整个 JSON 文件导入 Google Colab
【发布时间】:2020-05-19 20:03:02
【问题描述】:

我正在尝试将 json 文件从 github 导入到 google colab。它有效,但它没有从文件中读取所有列。这是我的代码:

import pandas as pd
url = 'https://raw.githubusercontent.com/lequanngo/WorldHappiness/master/WorldHappiness.json'
df = pd.read_json(url, orient='columns')
df.head(10)

这是结果:

country||ladder||ladderSD||Positive_affect||Negative_affect||SocialSupport||Freedom
Finland| |1| |4| |41| |10| |2| |5| 
Denmark
Norway
etc

',country,ladder,ladder_sd,positive_affect,negative_affect,social_support,freedom,corruption,generosity,gdp_per_capita,healthy_life_expectancy,continent\n0,Finland,1,4,41,10,2,5,4,47,22,27,Europe\n1'

显示所有 11 列(国家、阶梯、阶梯 SD、positve_affect、negative_affect 等)。但是当我通过使用获得描述性统计数据时

df.describe()
      |ladder|  |ladderSD|
count  156       156
mean   78.5      78.5
std
min
25%

只计算了阶梯和阶梯 SD。 Positive_affect 和negative_affect 以及所有其他连续数据列都没有考虑在内。

谁能帮我解决这个问题?

【问题讨论】:

    标签: python pandas dataframe google-colaboratory


    【解决方案1】:

    这是您期望的输出吗?

    >>> url = 'https://raw.githubusercontent.com/lequanngo/WorldHappiness/master/WorldHappiness.json'
    >>> df = pd.read_json(url, orient='records', dtype='dict')
    >>> df.head()                                                                                                                                                   
    
      Country (region)  Ladder  SD of Ladder Positive affect Negative affect  ... Freedom Corruption Generosity Log of GDP\nper capita Healthy life\nexpectancy
    0          Finland       1             4              41              10  ...       5          4         47                     22                       27
    1          Denmark       2            13              24              26  ...       6          3         22                     14                       23
    2           Norway       3             8              16              29  ...       3          8         11                      7                       12
    3          Iceland       4             9               3               3  ...       7         45          3                     15                       13
    4      Netherlands       5             1              12              25  ...      19         12          7                     12                       18
    
    [5 rows x 11 columns]
    
    >>> df.describe()                                                                                                                                               
    
               Ladder  SD of Ladder
    count  156.000000    156.000000
    mean    78.500000     78.500000
    std     45.177428     45.177428
    min      1.000000      1.000000
    25%     39.750000     39.750000
    50%     78.500000     78.500000
    75%    117.250000    117.250000
    max    156.000000    156.000000
    
    >>> df.describe(include='all')                                                                                                                                  
    
           Country (region)      Ladder  SD of Ladder  Positive affect  Negative affect  ...  Freedom  Corruption Generosity  Log of GDP\nper capita Healthy life\nexpectancy
    count               156  156.000000    156.000000            156.0            156.0  ...    156.0         156      156.0                     156                      156
    unique              156         NaN           NaN            156.0            156.0  ...    156.0         149      156.0                     153                      151
    top               Nepal         NaN           NaN            155.0            155.0  ...    155.0                  155.0                                                 
    freq                  1         NaN           NaN              1.0              1.0  ...      1.0           8        1.0                       4                        6
    mean                NaN   78.500000     78.500000              NaN              NaN  ...      NaN         NaN        NaN                     NaN                      NaN
    std                 NaN   45.177428     45.177428              NaN              NaN  ...      NaN         NaN        NaN                     NaN                      NaN
    min                 NaN    1.000000      1.000000              NaN              NaN  ...      NaN         NaN        NaN                     NaN                      NaN
    25%                 NaN   39.750000     39.750000              NaN              NaN  ...      NaN         NaN        NaN                     NaN                      NaN
    50%                 NaN   78.500000     78.500000              NaN              NaN  ...      NaN         NaN        NaN                     NaN                      NaN
    75%                 NaN  117.250000    117.250000              NaN              NaN  ...      NaN         NaN        NaN                     NaN                      NaN
    max                 NaN  156.000000    156.000000              NaN              NaN  ...      NaN         NaN        NaN                     NaN                      NaN
    
    [11 rows x 11 columns]
    
    

    【讨论】:

      猜你喜欢
      • 2019-06-20
      • 2021-06-06
      • 2022-08-06
      • 2020-11-28
      • 2019-08-03
      • 2020-12-17
      • 1970-01-01
      • 2014-10-04
      • 2020-08-21
      相关资源
      最近更新 更多