pandas和xlwings笔记
pip安装库
pip install pandas
pip install xlwings
pip install matplotlib
colums 是列
index是行
点线图示例
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus']=False
# Create a list of data to
# be represented in x-axis
subjects = [ 'Math' , 'English' , 'History' ,'Chem' , 'Geo' , 'Physics' , 'Bio' , 'CS' ]
# Create a list of data to be
# represented in y-axis
stress = [ 9 , 3 , 5 , 1 , 8 , 5 , 10 , 2 ]
# Create second list of data
# to be represented in y-axis
grades = [ 15 , 10 , 7 , 8 , 11 , 8 , 17 , 20 ]
# Create a dataframe using the three lists
df = pd.DataFrame(list(zip( stress , grades )),index = subjects ,columns = [ 'Stress' , 'Grades' ])
df.plot() #绘图
plt.show() #展示
删除文件示列
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
写入读取文本1
import os
fo = open("foo.txt", "r+")
fo.write("erwefsfsdffsdfr")
fo.close()
fo= open("foo.txt","r")
str=fo.read()
print("关键字",(str))
写入读取文本2
with open("ky.txt","w+",encoding="utf-8") as f:
f.write("fdsfsfdsf")
f.close()
f=open("ky.txt","r",encoding="utf-8")
print("字符串是",f.read())
读取表格,选择列,频次统计,写入表格
在python虚拟环境中,直接填同目录下的文件,是无法获取到的,只能填写绝对路径,非虚拟环境可以直接填。
```
cmouthbiaoge=pd.read_excel('宇宙奥秘.xlsx') #加载宇宙奥秘表格
print("nihao")
ziyuanchi=cmouthbiaoge['行星数量'] #选择行星数量列
print(ziyuanchi.value_counts())
with pd.ExcelWriter("宇宙奥秘.xlsx",mode='a',engine='openpyxl') as writer:
ziyuanchi.value_counts().to_excel(writer,sheet_name='星系') #将统计数据写入原工作簿的第二工作表
```
设置列宽
#app=xw.App(visible=False)
#wps=app.books.open('太阳的密码.xlsx')
#wps1=wps.activate
#liekuan1=wps.sheets['地球']
#liekuan1.range('d7').column_width=44
#wps.save()
#wps.close
#app.quit
计算两列时间差
haoshi=pd.read_excel('吃苹果过程.xlsx')
haoshi['开始吃的时间']=pd.to_datetime(haoshi['开始吃的时间'])
haoshi['吃完的时间']=pd.to_datetime(haoshi['吃完的时间'])
b=(haoshi['吃完的时间']-haoshi['开始吃的时间']).dt.seconds/60
with pd.ExcelWriter('吃苹果过程.xlsx',mode='a',engine='openpyxl',if_sheet_exists='overlay') as writer:
b.to_excel(writer,sheet_name='小明在吃苹果',startcol=45,startrow=1,index=None,header=False)