小编She*_*abu的帖子

Python:json规范化“字符串索引必须是整数”错误

我在以下代码中收到类型错误,如“类型错误:字符串索引必须为整数”。

import pandas as pd 
import json
from pandas.io.json import json_normalize

full_json_df = pd.read_json('data/world_bank_projects.json')
json_nor = json_normalize(full_json_df, 'mjtheme_namecode')
json_nor.groupby('name')['code'].count().sort_values(ascending=False).head(10)
Run Code Online (Sandbox Code Playgroud)
Output:
TypeError                                 
Traceback (most recent call last)
<ipython-input-28-9401e8bf5427> in <module>()
      1 # Find the top 10 major project themes (using column 'mjtheme_namecode')
      2 
----> 3 json_nor = json_normalize(full_json_df, 'mjtheme_namecode')
      4 #json_nor.groupby('name')['code'].count().sort_values(ascending = False).head(10)
TypeError: string indices must be integers
Run Code Online (Sandbox Code Playgroud)

python json normalization

5
推荐指数
1
解决办法
6876
查看次数

'NoneType' 对象没有属性 'fillna' 错误

我在以下代码中遇到错误。无法理解为什么。在代码的最后一行出现错误。请告知必须采取什么措施来纠正它。除此之外 df.isna().any() 也不起作用。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

data = pd.read_csv('BlackFriday.csv')
df = pd.DataFrame(data)

df.info()
df.describe()
df.head()

#To check the unique values of Product Categories 2, 3 and then assign a default value accordingly for NaN's

Product_Category_2 = df['Product_Category_2'].unique()
Product_Category_3 = df['Product_Category_3'].unique()
print('Product_Category_2', Product_Category_2)
print('Product_Category_3', Product_Category_3)

df = df.fillna(0, inplace=True)




---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-7f28e582ea84> in <module>()
      1 #We can replace the NaN's with 0.
----> 2 df …
Run Code Online (Sandbox Code Playgroud)

python pandas

2
推荐指数
1
解决办法
6494
查看次数

标签 统计

python ×2

json ×1

normalization ×1

pandas ×1