import pandas as pd
from pandas import Series,DataFrame
import numpy as np
# 創建Series對象並省略索引 一維數據'''
index 參數是可省略的,你可以選擇不輸入這個參數。
如果不帶 index 參數,Pandas 會自動用默認 index 進行索引,類似數組,索引值是 [0, ..., len(data) - 1]
'''
sel = Series([1,2,3,4])print(sel)01122334
dtype: int64
自定義創建索引 index=list 下面兩個例子效果是一樣的
sel = Series(data =[1,2,3,4], index =list('abcd'))print(sel)print('*'*20)
sel = Series(data =[1,2,3,4], index =['a','b','c','d'])print(sel)
a 1
b 2
c 3
d 4
dtype: int64
********************
a 1
b 2
c 3
d 4
dtype: int64
#(display現在可以理解爲同print顯示一樣 但效果更清楚一些)
display('索引下標',sel[['a','c']])print('*'*20)print('位置下標',sel[[1,3]])'索引下標'
a dlay
c lilei
dtype:object********************
位置下標 c lilei
a dlay
dtype:object
使用切片或取數據
# 索引切片 左右都包含
display('索引切片',sel['b':'d'])print('*'*20)#位置下標切片同list差不多 左包含右不包含print('位置切片',sel[1:3])'索引切片'
Series([], dtype:object)********************
位置切片 c lilei
b hanmeimei
dtype:object
重新賦值索引的值(ReIndex)
sel.index =list('dcba')print(sel)print('*'*20)# ReIndex重新索引,會返回一個新的Series 如果值爲int會自動轉成float64 如果字符轉成object# 利用reindex方法 重新設置索引 會根據以前索引按當前list索引排序 如果有值則賦值 無值則用NaNprint(sel.reindex(['b','a','c','d','e']))#排序規則 reindex
d xiaohai
c lilei
b hanmeimei
a dlay
dtype:object********************
b hanmeimei
a dlay
c lilei
d xiaohai
e NaN
dtype:object
'''
對 Series 的算術運算都是基於 index 進行的。
我們可以用加減乘除(+ - * /)這樣的運算符對兩個 Series 進行運算,
Pandas 將會根據索引 index,對響應的數據進行計算,結果將會以浮點數的形式存儲,以避免丟失精度。
如果 Pandas 在兩個 Series 裏找不到相同的 index,對應的位置就返回一個空值 NaN
'''
print(series1+series2)
Accra NaN
Delhi NaN
HongKong NaN
Humbai NaN
London 2.0
lagos 10.0
dtype: float64
相減
print(series1-series2)
Accra NaN
Delhi NaN
HongKong NaN
Humbai NaN
London 0.0
lagos -2.0
dtype: float64
相乘
print(series1*series2)
Accra NaN
Delhi NaN
HongKong NaN
Humbai NaN
London 1.0
lagos 24.0
dtype: float64
DataFrame (多維數組)
方法介紹
# DataFrame參數一:數據源 幾行幾列 # index 索引值# columns 列值
測試數據
df1 = DataFrame(np.random.randint(0,10,(4,4)),index=[1,2,3,4],columns=['a','b','c','d'])print(df1)
a b c d
10273231293888142059
字典轉DataFrame
默認字典轉換DataFrame (行索引由index決定,列索引由字典的鍵決定)
dict={'Province':['Guangdong','Beijing','Qinghai','Fujian'],'pop':[1.3,2.5,1.1,0.7],'year':[2018,2018,2018,2018]}
df2=pd.DataFrame(dict)# 沒有定義索引值 則是字典index值 也就是0開頭print(df2)print('*'*20)
df2=pd.DataFrame(dict,index=[1,2,3,4])# 此處定義索引值 則按自定義值顯示print(df2)
Province pop year
0 Guangdong 1.320181 Beijing 2.520182 Qinghai 1.120183 Fujian 0.72018********************
Province pop year
1 Guangdong 1.320182 Beijing 2.520183 Qinghai 1.120184 Fujian 0.72018
也可以使用from_dict (目前還不知道與上面有什麼區別)
dict2={"a":[1,2,3],"b":[4,5,6]}
df6=pd.DataFrame.from_dict(dict2)print(df6)
a b
014125236
索引相同的情況下,相同索引的值會相對應,缺少的值會添加NaN
data ={'Name':pd.Series(['zs','ls','we'],index=['a','b','c']),'Age':pd.Series(['10','20','30','40'],index=['a','b','c','d']),'country':pd.Series(['中國','日本','韓國'],index=['a','c','b'])}
df = pd.DataFrame(data)print(df)
Name Age country
a zs 10 中國
b ls 20 韓國
c we 30 日本
d NaN 40 NaN
df_dict ={'name':['James','Curry','Iversion'],'age':[18,20,19],'national':['us','China','us']}
df = pd.DataFrame(data=df_dict,index=['0','1','2'])print(df)
name age national
0 James 18 us
1 Curry 20 China
2 Iversion 19 us
df.info()<class'pandas.core.frame.DataFrame'>
Index:3 entries,0 to 2
Data columns (total 3 columns):
name 3 non-null object
age 3 non-null int64
national 3 non-null object
dtypes: int64(1),object(2)
memory usage:96.0+bytes
顯示頭幾行,默認顯示5行
print(df.head(2))
name age national
0 James 18 us
1 Curry 20 China
顯示後幾行
print(df.tail(1))
name age national
2 Iversion 19 us
獲取DataFrame的指定列 只獲取一列返回的就是一個 Series
display(df['name'])print(type(df['name']))0 James
1 Curry
2 Iversion
Name: name, dtype:object<class'pandas.core.series.Series'>
如果獲取多個列,那返回的就是一個 DataFrame 類型
print(df[['name','age']])print(type(df[['name','age']]))
name age
0 James 181 Curry 202 Iversion 19<class'pandas.core.frame.DataFrame'>
獲取一行
print(df[0:1])
name age national
0 James 18 us
獲取多行
print(df[1:3])
name age national
1 Curry 20 China
2 Iversion 19 us
獲取多行裏面的某一列(可以進行多行多列的選擇)
print(df[1:3][['name','age']])
name age
1 Curry 202 Iversion 19
獲取行數據
loc iloc 介紹
'''
df.loc 通過標籤索引獲取行數據
df.iloc 通過位置索引獲取行數據
'''
測試數據
df_dict ={'name':['James','Curry','Iversion'],'age':[18,20,19],'national':['us','China','us']}
df = pd.DataFrame(data=df_dict,index=['0','1','2'])print(df)
name age national
0 James 18 us
1 Curry 20 China
2 Iversion 19 us
loc 獲取某一行某一列的數據
print(df.loc['0','name'])
James
loc 一行所有列 (行,列)
print(df.loc['0',:])
name James
age 18
national us
Name:0, dtype:object
loc 某一行多列的數據
print(df.loc['0',['name','age']])
name James
age 18
Name:0, dtype:object
loc 選擇間隔的多行多列
print(df.loc[['0','2'],['name','national']])
name national
0 James us
2 Iversion us
loc 選擇連續的多行和間隔的多列 ----這裏注意 是連連續
print(df.loc['0':'2',['name','national']])
name national
0 James us
1 Curry China
2 Iversion us
iloc 取一行
print(df.iloc[1])
name Curry
age 20
national China
Name:1, dtype:object
iloc 取連續多行
print(df.iloc[0:2])
name age national
0 James 18 us
1 Curry 20 China
iloc 取間斷的多行
print(df.iloc[[0,2],:])
name age national
0 James 18 us
2 Iversion 19 us
df.iloc[0,0]='panda'print(df)
name age national
0 panda 18 us
1 Curry 20 China
2 Iversion 19 us
排序方法 ascending=False : 降序排列,默認是升序
df = df.sort_values(by='age',ascending=False)print(df)
name age national
1 Curry 20 China
2 Iversion 19 us
0 James 18 us
修改列行索引(如 都加_ABC)
測試數據
# 創建DataFrame對象 index爲行索引 columns爲列索引
df1 = pd.DataFrame(np.arange(9).reshape(3,3), index =['bj','sh','gz'], columns=['a','b','c'])print(df1)
a b c
bj 012
sh 345
gz 678
# 同時,rename 還可以傳入字典,爲某個 index 單獨修改名稱
df3 = df1.rename(index={'bj':'beijing'}, columns ={'a':'aa'})print(df3)
aa b c
beijing 012
sh 345
gz 678
行列值 轉化索引
測試數據
df1=pd.DataFrame({'X':range(5),'Y':range(5),'S':list("abcde"),'Z':[1,1,2,2,2]})print(df1)
X Y S Z
000 a 1111 b 1222 c 2333 d 2444 e 2
自定義行索引爲數據中其中一列 — 位置索引還在 (drop=False 指定同時保留作爲索引的列)
result = df1.set_index('S',drop=False)print(result)print('*'*20)
result.index.name=Noneprint(result)
X Y S Z
S
a 00 a 1
b 11 b 1
c 22 c 2
d 33 d 2
e 44 e 2********************
X Y S Z
a 00 a 1
b 11 b 1
c 22 c 2
d 33 d 2
e 44 e 2
行轉爲列索引
result = df1.set_axis(df1.iloc[0],axis=1,inplace=False)
display(result)print('*'*20)
result.columns.name=None
display(result)00 a 1000 a 1111 b 1222 c 2333 d 2444 e 2********************00 a 1000 a 1111 b 1222 c 2333 d 2444 e 2