正文
Windows下PythonQt3.2使用pandas.pivot_table
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
本机环境
1、win7 64 旗舰版
2、Qt 5.9.1(MSVC 2015,32 bit)
3、Python 3.7.1 (32-bit),二进制包安装的,即Windows x86 executable installer
4、PythonQt 3.2 使用上面的环境,源码编译
5、2018年10月26日
前叙
1、为什么没有全部使用vs2015编译python。就是官方建议的pythonQt和python的编译器保持一致,实际编译中python的库编译成功了,但应该是环境问题,和官方的安装包比不是很一样。并且vs2015只能支持编译Python3.6及以下版本。
2、为什么没有使用Anaconda进行管理。本文中使用pandas包,先前打算使用Pythoner都说pip怎么怎么麻烦,推荐的Anaconda进行包的管理,经过摸索发现,Anaconda里面包含了Python,根本不需要先安装Python,Anaconda都会内置一个Python(这个python才拥有各种包的环境),并且Anaconda不是每个python都有一个版本的。
Anaconda3-5.3.0-Windows-x86——内置python-3.7.0
Anaconda3-5.2.0-Windows-x86——内置python-3.6.5
我在编译PythonQt3.2时,设置了Anaconda中python库的位置,编译完全没有问题,但运行实例时,报错(具体错误忘记),但是使用对应版本的Python二进制安装包安装,然后编译PythonQt3.2运行实例,则正常,也就是说PythonQt3.2无法使用Anaconda提供的python库。——有解决方法,欢迎各位指正
安装pandas
通过参考资料,因为国内被墙,pip使用国内镜像源,安装pandas,还好pip会自己安装pandas依赖的包。CMD中运行:
pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
此时配置Pycharm,可以看到已安装的包,其实也可以从pycharm中安装包,就是下图右侧稍微偏上的+ - 号那里
测试代码:
要注意PythonQt3.2中import pandas会报没有模块,但是在cmd中可以。根据参考资料,包含路径即可解决,本人也尝试将这个路径加入环境变量Path,但不好用。怎么查看三方包site-packages的目录,见下图:
源码:
from PythonQt.QtGui import *
import sys
# To use site-packages
sys.path.append('C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages')from collections import OrderedDict
from pandas import DataFrame
import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * ,
'B': ['A', 'B', 'C'] * ,
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * ,
'D': ['甲', '乙', '丙'] * ,
'E': [datetime.datetime(, i, ) for i in range(, )] +
[datetime.datetime(, i, ) for i in range(, )]})
print(df)
p = pd.pivot_table(df,index=['A', 'B'], columns=['C','D'],aggfunc='count',fill_value='',margins=True)
print(p)
运行结果:
A B C D E
one A foo 甲 --
one B foo 乙 --
two C foo 丙 --
three A bar 甲 --
one B bar 乙 --
one C bar 丙 --
two A foo 甲 --
three B foo 乙 --
one C foo 丙 --
one A bar 甲 --
two B bar 乙 --
three C bar 丙 --
one A foo 甲 --
one B foo 乙 --
two C foo 丙 --
three A bar 甲 --
one B bar 乙 --
one C bar 丙 --
two A foo 甲 --
three B foo 乙 --
one C foo 丙 --
one A bar 甲 --
two B bar 乙 --
three C bar 丙 --
E
C bar foo All
D 丙 乙 甲 丙 乙 甲
A B
one A
B
C
three A
B
C
two A
B
C
All
后记:
PythonQt3.2中import pandas报没有模块,但是在cmd中可以,模块路径也在path中。是因为PythonQt初始化时
修改
PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
====>
PythonQt::init( PythonQt::RedirectStdOut);
参考资料
更换pip源到国内镜像 https://blog.csdn.net/chenghuikai/article/details/55258957
在Qt(C++)中与Python混合编程 https://www.cnblogs.com/xia-weiwen/p/7932379.html
Pycharm和Anaconda中如何安装package https://blog.csdn.net/chaowanghn/article/details/54017331