1.3檢視列、行、單元格
為啥要用python中的pandas庫進行資料分析,用excel不可以嗎?不可以,excel處理上萬條資料時通常會宕機或者出錯,python不會有這種問題。相信鯉魚學長,在學習乃至日後工作中,pandas庫將會風靡相當長一段時間。
示例:pandas 是基於numpy 的一種工具,該工具是為了解決資料分析任務而建立的。
**如下(示例):
**如下(示例):import pandas
import pandas as pd#這裡是給pandas函式取了乙個名字:pd,後面呼叫時寫pd就行。
1、預設情況下,read_csv函式會讀取逗號分隔檔案。df=pd.
read_csv
(r'c:\users\wly\desktop\python資料分析(活用pandas庫)\pandas_for_everyone-master\data\gapminder.tsv'
,sep=
'\t'
(df.
head()
)
2、這裡將sep引數設定為\t,是指明使用製表符分隔的意思。
3、呼叫head()方法,只顯示前5行資料。
執行結果如下:
country continent year lifeexp pop g***ercap
0 afghanistan asia 1952
28.801
8425333
779.445314
1 afghanistan asia 1957
30.332
9240934
820.853030
2 afghanistan asia 1962
31.997
10267083
853.100710
3 afghanistan asia 1967
34.020
11537966
836.197138
4 afghanistan asia 1972
36.088
13079460
739.981106
執行結果如下:df=pd.
read_csv
(r'c:\users\wly\desktop\python資料分析(活用pandas庫)\pandas_for_everyone-master\data\gapminder.tsv'
,sep=
'\t'
(type
(df)
)
>
執行結果如下:
(df.shape)
也就是說這個資料集有1704行,6列。(
1704,6
)
執行結果如下:
(df.columns)
這裡可以看到,列名的型別是object。index([
'country'
,'continent'
,'year'
,'lifeexp'
,'pop'
,'g***ercap'
], dtype=
'object'
)
執行結果如下:
(df.dtypes)
pandas型別country object
continent object
year int64
lifeexp float64
pop int64
g***ercap float64
dtype: object
python型別
說明object
string
最常用的資料型別
int64
int整型
float64
float
帶小數的數字
datatime64
datatime
python標準庫里包含datatime,但是預設不載入,需要匯入才能用
獲取資料中的某列,比如獲取country列。,這裡將她儲存到乙個變數裡。
執行結果如下:country_df=df[
'country'
(country_df.
head()
)#顯示前5行
(country_df.
tail()
)#顯示後5行
通過列名獲取多列。#顯示前5行
0 afghanistan
1 afghanistan
2 afghanistan
3 afghanistan
4 afghanistan
name: country, dtype: object
#顯示後5行
1699 zimbabwe
1700 zimbabwe
1701 zimbabwe
1702 zimbabwe
1703 zimbabwe
name: country, dtype: object
執行結果如下:subset=df[
['country'
,'continent'
,'year']]
(subset.
head()
(subset.
tail()
)
我用的python版本是python3.7 64位,pycharm是2017.1 64位。大家根據自己的電腦來安裝python和編譯器。country continent year
0 afghanistan asia 1952
1 afghanistan asia 1957
2 afghanistan asia 1962
3 afghanistan asia 1967
4 afghanistan asia 1972
country continent year
1699 zimbabwe africa 1987
1700 zimbabwe africa 1992
1701 zimbabwe africa 1997
1702 zimbabwe africa 2002
1703 zimbabwe africa 2007
python資料分析1
在github上發現了乙個不錯的教程 雖然是全英文的,找了下沒有翻譯的版本,但還是決定看這個。寫個部落格記些筆記。昨天晚上看了第一張引子,大概說了下為啥用python,jupyter,他們的好處和新特性,以及如何安裝,沒有啥特別的,中文講這內容的很多。1.1 直接通過anaconda可以開啟jupy...
python資料分析基礎1
filter 函式用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表 接收兩個引數,第乙個為函式,第二個為序列,序列的每個元素作為引數傳遞給函式進行判斷,返回true或false,將返回true的元素放到新列表中。def ou n if n 2 0 return true else...
小白學 Python 資料分析(1) 資料分析基礎
人工植入廣告 ps 小編最近兩天偷了點懶,好久沒有發原創了,最近是在 csdn 開通了乙個付費專欄,用來發布去年寫的沒有出版的書稿,感興趣的同學可以去看下 已經上傳了一部分,第一章設定為了試讀章節 主要是講 springcloud 微服務方面的一些內容,整體排版下來如果是印在實體書上應該會超過 40...