接上篇python模块基础一
(1)下载好pandas后导入pandas模块
import pandas as pd
(2)# 使用pandas的DataFrame的二維表格数据结构,就相当于一个Excel表 具体操作上代码:(注释标的那么明白,朋友,你不能挑我理吧)
a = pd.DataFrame([[1,2],[3,4],[5,6]])
print(a)
'''有行索引和列索引
0 1
0 1 2
1 3 4
2 5 6
'''
a= pd.DataFrame([[1,2],[3,4],[5,6]],columns=['data','score'],index=['A','B','c'])
print(a)
'''
data score
A 1 2
B 3 4
c 5 6
'''
a=pd.DataFrame()
data1 = pd.read_excel('./py基础练习2.xlsx',sheet_name=0)
print(data1)
app1=xw.App(visible=True,add_book=False)
workBook1=app1.books.add()
workBook2=app1.books.add()
workSheet1=workBook1.sheets.add('py基础练习4-pandas')
workSheet2 = workBook2.sheets.add('test_page')
workBook1.save(r'F:\python\py基础练习4.xlsx')
workBook1.close()
workBook2.save(r'F:\python\py基础练习4_1.xlsx')
workBook2.close()
app1.quit()
data2 = pd.DataFrame([['2021/9/9',1],['2021/9/10',1],['2021/9/11',1]],columns=['时间','打卡情况'])
data2.to_excel(r'F:\python\py基础练习4.xlsx')
data3 = pd.DataFrame([['2021/9/9',0],['2021/9/10',0],['2021/9/11',0]],columns=['时间','打卡情况'])
data3.to_excel(r'F:\python\py基础练习4.xlsx',columns=['时间','打卡情况'],index=False)
print(pd.read_excel('./py基础练习4.xlsx '))
data = pd.DataFrame([[1,2,3],[3,4,5],[5,6,7]],columns=['a','b','c'],index=['1','2','3'])
data.to_excel(r'F:\python\py基础练习4_1.xlsx')
a = data['a']
b = data[['a','c']]
print(a,'a列数据')
print(b,'a列c列数据')
a = data[1:3]
print(a,'第0、1行数据')
a=data[['a','c']][0:2]
print(a,'选取a和c列的第0行、第一行')
a= data[data['a']>1]
print(a,'a列中数据大于1的行')
c= data[(data['a']>1) & (data['c']>5)]
print(c,'a列大于1,且c列大于5的行')
a= data.sort_values(by='c',ascending=False)
print(a,'根据c列降序排序')
data['d'] = data['c']+ data['b']
data.to_excel(r'F:\python\py基础练习4_1.xlsx')
print(data,'b列和c列相加生成d列')
df1= pd.DataFrame({'姓名':['林','张','王'],'分数':[888,666,555]})
df2= pd.DataFrame({'姓名':['林','张','李'],'年龄':[21,26,30]})
print(df1,'df1的值')
print(df2,'df2的值')
df3 = pd.merge(df1,df2)
print(df3,'df1和df2连接一起的数据表')
df4 = pd.merge(df1,df2,how='left')
print(df4,'保留左表df1')
df5=pd.merge(df1,df2,left_index=True,right_index=True)
print(df5,'按照索引进行合并')
df6=pd.concat([df1,df2])
print(df6,'df6纵向拼接')
df7=pd.concat([df1,df2],ignore_index=True)
print(df7,'拼接后忽略索引然后新建索引')
df8=pd.concat([df1,df2],axis=1)
print(df8)
df9 = df1.append({'姓名':['郭'],'分数':[888]},ignore_index=True)
print(df9)
(3)数据可视化模块Matplotlib(记得导入前下载呀兄dei,万无一失的下载方法)
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6]
y = [2,4,6,8,10,12]
plt.plot(x,y)
plt.show()
plt.bar(x,y)
plt.show()
ps:如果对你有帮助的化,不妨给俺点个赞呗。
|