1、數據:
鏈接:https://pan.baidu.com/s/1xl2h1I8O8E2xvzytEuPmFQ
提取碼:fcrn
2、從不同的維度分析數據:
公司維度、年齡維度、不同分數段
3、以下爲具體代碼:
#導入庫
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#讀取文件
luohu_data = pd.read_csv('./bj_luohu.csv',index_col = 'id')
#describe()展示一些基本信息
luohu_data.describe()
#### read_csv參數:dtype 可以指定整個DataFrame或各個列的數據類型;通過指定name與header,可以重命名列以及是否丟棄標題行;usecols參數允許您使用列名,位置號或可調用的方法選擇文件中列的任何子集;如果指定了comment參數,則將忽略註釋行。 默認情況下,空行也將被忽略,如果skip_blank_lines = False,則read_csv將不會忽略空行;index_col是read_csv中的一個參數。用來指定表格的索引值,在默認爲None的時候,pandas會自動將第一列作爲索引,並額外添加一列。所以大多我們會使用index_col=0
,直接將第一列作爲索引,不額外添加列;
#查看下數據
luohu_data
3.1 從公司維度分析下,可以查看那個公司落戶人數多
#按照company分組並計算每組個數
#groupby默認會把by的這個列作爲索引列返回,可以設置下as_indx=False
company_data = luohu_data.groupby('company').count()
company_data
as_indx=False時結果:
爲了後續方便統計,將name重命名爲people_count
#重命名列名稱
# pandas 中 inplace 參數在很多函數中都會有,它的作用是:是否在原對象基礎上進行修改
# inplace = True:不創建新的對象,直接對原始對象進行修改;
# inplace = False:對數據進行修改,創建並返回新的對象承載其修改結果。
# 默認是False,即創建新的對象進行修改,原對象不變,和深複製和淺複製有些類似。
company_data1.rename(columns={'name':'people_count'},inplace = True)
#按某一列排序
#sort_values對列進行排序的 即對values進行排序
company_sorted_data = company_data1.sort_values('people_count',ascending=False)
#按條件過濾
#只有一人的公司
company_sorted_data[company_sorted_data['people_count']==1]
#人數前50的公司
company_sorted_data.head(50)
由此可見:積分落戶人數最多的是北京華爲數字技術公司。
3.2 下面從分數維度分析下,通過describe函數,可以得知分數最小爲90.75,最大爲122.59
#分數分佈
#按照步長5分桶統計下分數的分佈
bins = np.arange(90,130,5)
bins = pd.cut(luohu_data['score'],bins)
#統計分組下的個數
bins_counts = luohu_data['score'].groupby(bins).count()
#處理index
bins_counts.index=[str(x.left)+'~'+str(x.right) for x in bins_counts.index]
bins_counts.plot(kind='bar',alpha=1,rot=0)
plt.show()
可見90-95分人數是最多的。
3.3 年齡分析
#年齡分佈
#出生日期轉爲年齡
import time
now_time = time.strftime('%Y-%m',time.localtime(time.time()))
now_time = pd.to_datetime(now_time)
luohu_data['age']=((now_time
-pd.to_datetime(luohu_data['birthday'])))/ pd.Timedelta('365 days')
bins = np.arange(20,70,5)
bins = pd.cut(luohu_data['age'],bins)
bins_counts=luohu_data['age'].groupby(bins).count()
bins_counts.index=[str(x.left)+'~'+str(x.right) for x in bins_counts.index]
bins_counts.plot(kind='bar',alpha=1,rot=0)
plt.show()