除非 Dataframe 保存到csv中并作为csv读回,否则Pandas不会过滤,源是加载到 Dataframe 中的JSON

kpbwa7wx  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(130)

我有一个json输出,如下所示。

{'pagination': {'limit': 100, 'offset': 0, 'count': 38, 'total': 38},
 'data': [{'name': 'Ceco Environmental Corp',
   'symbol': 'CECE',
   'has_intraday': False,
   'has_eod': True,
   'country': None,
   'stock_exchange': {'name': 'NASDAQ Stock Exchange',
    'acronym': 'NASDAQ',
    'mic': 'XNAS',
    'country': 'USA',
    'country_code': 'US',
    'city': 'New York',
    'website': 'www.nasdaq.com'}},
  {'name': 'CEC CoreCast Corporation Ltd',
   'symbol': '600764.XSHG',
   'has_intraday': False,
   'has_eod': True,
   'country': None,
   'stock_exchange': {'name': 'Shanghai Stock Exchange',
    'acronym': 'SSE',
    'mic': 'XSHG',
    'country': 'China',
    'country_code': 'CN',
    'city': 'Shanghai',
    'website': 'www.sse.com.cn'}},
  {'name': 'CECEP WindPower Corp',
   'symbol': '601016.XSHG',
   'has_intraday': False,
   'has_eod': True,
   'country': None,
   'stock_exchange': {'name': 'Shanghai Stock Exchange',
    'acronym': 'SSE',
    'mic': 'XSHG',
    'country': 'China',
    'country_code': 'CN',
    'city': 'Shanghai',
    'website': 'www.sse.com.cn'}},
  {'name': 'CECONOMY AG INHABER-STAMMAKTIEN O.N.',
   'symbol': 'CEC.XSTU',
   'has_intraday': False,
   'has_eod': True,
   'country': None,
   'stock_exchange': {'name': 'Börse Stuttgart',
    'acronym': 'XSTU',
    'mic': 'XSTU',
    'country': 'Germany',
    'country_code': 'DE',
    'city': 'Stuttgart',
    'website': 'www.boerse-stuttgart.de'}},
  {'name': 'CECONOMY AG ST O.N.',
   'symbol': 'CEC.XFRA',
   'has_intraday': False,
   'has_eod': True,
   'country': None,
   'stock_exchange': {'name': 'Deutsche Börse',
    'acronym': 'FSX',
    'mic': 'XFRA',
    'country': 'Germany',
    'country_code': 'DE',
    'city': 'Frankfurt',
    'website': 'www.deutsche-boerse.com'}},
  {'name': 'CECONOMY AG ST O.N.',
   'symbol': 'CEC.XETRA',
   'has_intraday': False,
   'has_eod': True,
   'country': None,
   'stock_exchange': {'name': 'Deutsche Börse Xetra',
    'acronym': 'XETR',
    'mic': 'XETRA',
    'country': 'Germany',
    'country_code': 'DE',
    'city': 'Frankfurt',
    'website': ''}},
  {'name': 'CECEP COSTIN',
   'symbol': '2228.XHKG',
   'has_intraday': False,
   'has_eod': True,
   'country': None,
   'stock_exchange': {'name': 'Hong Kong Stock Exchange',
    'acronym': 'HKEX',
    'mic': 'XHKG',
    'country': 'Hong Kong',
    'country_code': 'HK',
    'city': 'Hong Kong',
    'website': 'www.hkex.com.hk'}},
.....

我正在尝试将其加载到 Dataframe 中,并按国家筛选stock_exchange列。
这是我的代码。

import pandas as pd
data = api_result.json()
result = pd.DataFrame(data['data'])

result[result['stock_exchange'].str.contains('China')]

但是我得到了以下错误,KeyError: "None of [Float64Index([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,\n nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,\n nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],\n dtype='float64')] are in the [columns]"
但是,如果我将输出保存为csv,然后将其重新加载到这样的 Dataframe 中,

result.to_csv('result.csv')
result = pd.read_csv('result.csv')

result[result['stock_exchange'].str.contains('China')]

我得到了这样的过滤 Dataframe

Unnamed: 0  name    symbol  has_intraday    has_eod country stock_exchange
1   1   CEC CoreCast Corporation Ltd    600764.XSHG False   True    NaN {'name': 'Shanghai Stock Exchange', 'acronym':...
2   2   CECEP WindPower Corp    601016.XSHG False   True    NaN {'name': 'Shanghai Stock Exchange', 'acronym':...

你知道为什么我不能过滤 Dataframe 没有保存帧到csv和重新加载第一?

uplii1fm

uplii1fm1#

必须重新创建 Dataframe 。请从以下代码使用

df = pd.DataFrame.from_records(data.get('data'))
4jb9z9bj

4jb9z9bj2#

我应该注意到,stock_exchange的每个单元格都是一个dict对象,而不是str对象,这是问题的一部分,因为您将始终获得NaN而不是bool,因此您不能像当前所做的那样使用布尔索引-即result[result['stock_exchange'].str.contains('China')]

df = pd.json_normalize(data['data'])
df[df['stock_exchange.country'] == 'China']

                           name       symbol  has_intraday  has_eod country  \
1  CEC CoreCast Corporation Ltd  600764.XSHG         False     True    None   
2          CECEP WindPower Corp  601016.XSHG         False     True    None   

       stock_exchange.name stock_exchange.acronym stock_exchange.mic  \
1  Shanghai Stock Exchange                    SSE               XSHG   
2  Shanghai Stock Exchange                    SSE               XSHG   

  stock_exchange.country stock_exchange.country_code stock_exchange.city  \
1                  China                          CN            Shanghai   
2                  China                          CN            Shanghai   

  stock_exchange.website  
1         www.sse.com.cn  
2         www.sse.com.cn

相关问题