Mac 安装 pip3 install pandas
如果说numpy是一个列表的话,pandas更像是一个字典
两个一般会搭配使用
import pandas as pd
import numpy
s = pd.Series([1,3,6,np.nan,44,1])
dates = pd.date_range('20190106', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['a','b','c','d'])
df1 = pd.DataFrame(np.arange(12).reshape((3,4)))
df2 = pd.DataFrame({'A':1.,
'B': pd.Timestamp('20130102'),
'C': pd.Series(1, index=list(range(4)), dtype='float32'),
'D': np.array([3] * 4, dtype='int32'),
'E': pd.Categorical(["test","train","test","train"]),
'F': 'foo'})
df2.dtype
image.png
image.png
image.png
image.png
image.png
image.png
image.png
选择数据
dates = pd.date_range('20190106', periods=6)
df = pd.DataFrame(np.arrange(24).reshape((6,4)), index=dates, columns=['A','B','C','D'])
print(df['A'], df.A)
print(df.loc['20190106]) #使用标签选择数据
print(df.loc[:, ['A','B']]) #选择列标签,打印对应列的整行内容
print(df.iloc[3, 1]) #选择第3行第一列
print(df.iloc[[]1,3,5], 1:3) #切片处理
print(df[df.A > 8) #筛选元素大于8的数据
修改数据
df.iloc[2,2] = 111
df.loc['20190106', 'B'] = 222
df.B[df.A>4] = 0 #筛选数据并更改值
- pandas可以读取很多类型的文件,常用的是csv
import pandas as pd
data = =pd.read_csv('student.csv')
print(data)
合并多个dataFrame
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.ones((3,4))*0, columns=['a','b','c','d'], index=[1,2,3])
df2 = pd.DataFrame(np.ones((3,4))*0, columns=['b','c','d','e'], index=[2,3,4])
res = pd.concat([df1, df2], axis=0, ignore_index=True)
res = pd.concat([df1,df2], join='inner', ignore_index=True) #inner是求交集,outer是求outer
res = pd.concat([df1,df2], axis=1, join_axes=[df1.index]) # 1是左右合并
res = df1.append(df2, ignore_index=True)
res = df1.append([df2,df3], ignore_index=True)
合并merge
plot
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# plot
#Series
data = pd.Series(np.random.randn(1000), index=np.arange(1000))
data = data.cumsum()
data.plot()
plt.show()
#DataFrame
data = pd.DataFrame(np.randn(1000, 4),
index=np.arange(1000),
columns=list("ABCD"))
data = data.cumsum()
#print(data.head())#打印前几个数据,默认是5
data.plot()
plt.show()
#scatter
#ax代表axes,代表一个绘图窗口
ax = data.plot.scatter(x='A',y='B', color='DarkBlue', label='Class 1')
data.plot.scatter(x='A',y='C',color='DarkGreen',label='Class 2', ax=ax)
plot.show()
plot方法一共有
- bar
- hist
- box
- kde
- area
- acatter
- hexbin
- pie
可以生成类似这样的图
image.png image.png image.png
网友评论