我在以下代码中收到类型错误,如“类型错误:字符串索引必须为整数”。
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) 我在以下代码中遇到错误。无法理解为什么。在代码的最后一行出现错误。请告知必须采取什么措施来纠正它。除此之外 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)