import matplotlib.pyplot as plt
import numpy as np
plt.rcParams[‘font.sans-serif’] = [‘SimHei’]#正常显示中文标签
plt.rcParams[‘axes.unicode_minus’] = False#正常显示负号
import seaborn as sns
import pandas as pd
data = {
‘apples’:[3,2,0,1],
‘oranges’:[0,1,2,3],
‘banans’:[1,2,1,0]
}
df = pd.DataFrame(data,index=[‘June’,‘Robert’,‘Lily’,‘David’])
df
apples oranges banans
June 3 0 1
Robert 2 1 2
Lily 0 2 1
David 1 3 0
#访问行 切边用于取行
df[0:3]
apples oranges banans
June 3 0 1
Robert 2 1 2
Lily 0 2 1
df[:1]
apples oranges banans
June 3 0 1
df[True,False,False,False]
#通过query方法访问行 输出满足条件的行
df.query(‘apples>1’)
apples oranges banans
June 3 0 1
Robert 2 1 2
#用存取器loc[]
DataFrame.loc[n,m]
df.loc[‘David’,‘apples’]
df.loc[[‘David’,‘Robert’],‘apples’]
David 1
Robert 2
Name: apples, dtype: int64
#DataFrame.iloc[n,m] 是位置
df.iloc[3,0]
df
df.iloc[[3,1],0]
David 1
Robert 2
Name: apples, dtype: int64
df.iloc[:,0:2]
apples oranges
June 3 0
Robert 2 1
Lily 0 2
David 1 3
#at[] iat[]取单个元素
#行的添加 append
datas={‘apples’:[1,2],
‘oranges’:[3,4],
‘banans’:[1,1]}
dfs = pd.DataFrame(datas,index=[‘Robert’,‘tom’])
df.append(dfs)
apples oranges banans
June 3 0 1
Robert 2 1 2
Lily 0 2 1
David 1 3 0
Robert 1 3 1
tom 2 4 1
#行删除
df.drop(‘Robert’)
#列的添加和删除
#del
#pop
del df[‘apples’]
df
df.pop(‘banans’)