2022-02-20
阅读量:
750
python数据清洗案例代码(速查 helpfull!!!)<欧洲城市人口普查>
1. 导入数据
df=pd.read_csv('.csv')
df.head()
2. 初步探索数据
2.1. 查看数据类型
df.info()
2.2. 查看Shape
df.shape
2.3. 是否有缺失值
3. 对数据进行清洗
3.1. 城市
df[''].unique()
df['']=df[''].str.strip('[2107]')
3.2. 国家
df['']=df[''].str.strip()
3.3. 人口
df['']=df[''].str.replace(',','').astype(int32)
3.4. 日期
df['']=pd.to_datetime(df[''])
3.5. 保存数据
df.to_csv('.csv')
4. 数据分析
4.1. 重新设置索引(将排名设置为索引)
df.reset_index('Rank',inplace=True)
df.reset_index(drop=True, inplace=True)
df.head(3)
df.index=df.index+1
df.head(3)
4.2. 查看哪个国家城市最多
df['State'].value_counts()
df['State'].value_counts()[0:10].plot(kind='barh')
4.3. 查看哪个国家人口最多
df[['State','Population']].groupby('State').sum().sort_values('Population'ascending=False)
df1=df[['State','Population']].pivot_table(index='State',aggfunc='sum').nlargest(10,columns='Population')
df1
df1.plot(kind='barh')






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论