[Python] study notes 10-pandas library

pandas library

Insert picture description here

Insert picture description here

Series

Insert picture description here

import numpy as np
import pandas as pd

#Series的三种创建方法
print(pd.Series([1,2],index=['北京','上海']))
print('---------------------')

print(pd.Series(np.arange(3,6)))
print('---------------------')

print(pd.Series({
    
    '北京':11,'上海':12,'深圳':13}))

result:
Insert picture description here
Insert picture description here

import numpy as np
import pandas as pd

obj=pd.Series(np.arange(3,7))
print(obj.values)
print('-----------')
print(obj.index)
print('-----------')
print(obj.array)
print('-----------')
print(obj[0])
print('-----------')
print(obj[[0,1,3]])
print('-----------')
obj[1]=99
print(obj[[0,1,3]])

result:
Insert picture description here

Important function: automatic alignment

import numpy as np
import pandas as pd

obj1=pd.Series({
    
    '北京':1,'上海':2,'广州':3})
obj2=pd.Series({
    
    '上海':4,'英国':1,'北京':9})

print(obj1+obj2)

result:
Insert picture description here
Insert picture description here

import numpy as np
import pandas as pd

obj1=pd.Series({
    
    '北京':1,'上海':2,'广州':3})
obj1.name='人数'
obj1.index.name='表1'

print(obj1)

result:
Insert picture description here

DataFrame

Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
frame=pd.DataFrame(data)
print(frame)

result:
Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#也可以制定顺序
frame=pd.DataFrame(data,columns=['sex','name','age'])
print(frame)

result:
Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#如果列名没找到,就会是NaN
frame=pd.DataFrame(data,columns=['sex','name','age','num'])
print(frame)

result:
Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#如果列名没找到,就会是NaN
frame=pd.DataFrame(data,columns=['sex','name','age'],index=['one','two','three','four'])
print(frame)
print('----------------')
#可以获得一个Series
print(frame['name'])

result:
Insert picture description here

Can be assigned and modified

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#如果列名没找到,就会是NaN
frame=pd.DataFrame(data,columns=['sex','name','age','num'])
frame['num']=1
print(frame)

result:
Insert picture description here
Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#如果列名没找到,就会是NaN
frame=pd.DataFrame(data,columns=['sex','name','age','num'])
frame['num']=([1,2],[2,3],[4,5],[1,4])
print(frame)

result:
Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#如果列名没找到,就会是NaN
frame=pd.DataFrame(data,columns=['sex','name','age','num'])
frame['num']=pd.Series([1,2,3],index=[1,2,3])
print(frame)

result:
Insert picture description here

Create new column

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#如果列名没找到,就会是NaN
frame=pd.DataFrame(data,columns=['sex','name','age','num'])
frame['if']=frame.name=='zy'
print(frame)

result:
Insert picture description here

Delete column

import numpy as np
import pandas as pd

data={
    
    'name':['zy','lhr','zym','zyy'],'sex':['f','m','f','f'],'age':['18','19','20','21']}
#如果列名没找到,就会是NaN
frame=pd.DataFrame(data,columns=['sex','name','age','num'])
frame['if']=frame.name=='zy'
del frame['if']
print(frame)

result:
Insert picture description here

Nested dictionary

Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':{
    
    1:'zy',2:'lhr'},'sex':{
    
    2:'f',3:'m'}}
frame=pd.DataFrame(data)
print(frame)
print('---------')
#可以转置
print(frame.T)

result:
Insert picture description here

Set the name of index and columns

import numpy as np
import pandas as pd

data={
    
    'name':{
    
    1:'zy',2:'lhr'},'sex':{
    
    2:'f',3:'m'}}
frame=pd.DataFrame(data)
#设置index和column的名字
frame.index.name='aaa'
frame.columns.name='bbb'
print(frame)

result:
Insert picture description here

return

Insert picture description here

import numpy as np
import pandas as pd

data={
    
    'name':{
    
    1:'zy',2:'lhr'},'sex':{
    
    2:'f',3:'m'}}
frame=pd.DataFrame(data)
print(frame.values)

result:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/Shadownow/article/details/105867950