pandas系列之-Series

最新這段時間最學習pandas,這個系列將對pandas的知識點進行總結歸納,趁熱打鐵


###series###

import numpy as np
import pandas as pd
labels = ['a','b','c']
my_list = [10,20,30]
arr = np.array([10,20,30])
d = {'a':10,'b':20,'c':30}

pd.Series(data=my_list)

0 10
1 20
2 30
dtype: int64

pd.Series(data=my_list,index=labels)

a 10
b 20
c 30
dtype: int64

pd.Series(my_list,labels)

a 10
b 20
c 30
dtype: int64

pd.Series(arr)

0 10
1 20
2 30
dtype: int64

pd.Series(arr,labels) 

a 10
b 20
c 30
dtype: int64

pd.Series(d)

a 10
b 20
c 30
dtype: int64

pd.Series(data=labels)

0 a
1 b
2 c
dtype: object

pd.Series([sum,print,len])

0 <built-in function sum>
1 <built-in function print>
2 <built-in function len>
dtype: object

ser1 = pd.Series([1,2,3,4],index = ['USA', 'Germany','USSR', 'Japan']) 
ser1

USA 1
Germany 2
USSR 3
Japan 4
dtype: int64

ser2 = pd.Series([1,2,5,4],index = ['USA', 'Germany','Italy', 'Japan']) 
ser2

USA 1
Germany 2
Italy 5
Japan 4
dtype: int64

ser1['USA']

1

ser1 + ser2

Germany 4.0
Italy NaN
Japan 8.0
USA 2.0
USSR NaN
dtype: float64

s4 =  pd.Series([1,2,3,4], index = ['a','b','c','d']
s4

a 1
b 2
c 3
d 4
dtype: int64

s4.values

array([1, 2, 3, 4], dtype=int64)

s4.index

Index([‘a’, ‘b’, ‘c’, ‘d’], dtype=‘object’)

s4['a']

1

s4[s4>2]

c 3
d 4
dtype: int64

s4.to_dict() 

{‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4}

s5 = pd.Series(s4.to_dict())
s5

a 1
b 2
c 3
d 4
dtype: int64

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章