
python
默默前行的蜗牛
Data Mining ML
展开
-
在python3.6上访问impala数据库
在python3.6上访问impala数据库安装impyla包 cmd: python3 -m pip install impyla 在安装的过程中可能会报出Microsoft Visual C++ 14.0 is required 按照提示的链接下载2015版,然后再重新安装就OK代码demofrom impala.dbapi import connectfrom impala.u原创 2017-06-07 16:43:21 · 11005 阅读 · 0 评论 -
[python笔记]完善describe()
def stats(x): return pd.Series([x.count(),x.min(),x.idxmin(), x.quantile(.25),x.median(), x.quantile(.75),x.quantile(.95),x.mean(), x.max(),x.idxmax(), x.mad(),x.var(), x.std(),原创 2017-08-16 10:04:33 · 4305 阅读 · 0 评论 -
[python笔记]行列转换,数据透视
import pandas as pdimport numpy as npa0=[['亨利','男','语文',98.],['C罗','男','体育',60.],['亨利','男','数学',60.],['C罗','男','语文',100.]]af0 = pd.DataFrame(a0,columns=['name','sex','lesson','score'])# print(af0)#原创 2017-08-16 10:05:00 · 1475 阅读 · 0 评论 -
[python]Scatter 散点图例子
import numpy as npimport matplotlib.pyplot as pltimport os,sys# print(os.getcwd())#显示当前目录#读取数据data = np.loadtxt('../../data/data1.txt',delimiter=',')x=data[:,0:2]y=data[:,2]pos = np.where(y==1)原创 2017-08-17 18:19:43 · 1499 阅读 · 0 评论 -
蒙特卡洛模拟计算PI
import randomimport timenum=1for i in range(3,6): print('第{0}次试验'.format(num)) start = time.clock() count = 0 for j in range(10**i): x = random.uniform(-1,1) y = ra原创 2017-08-15 17:01:43 · 2118 阅读 · 0 评论 -
java调用Python遇到的一系列问题
1.jython在eclipse控制台出现Failed to install ”: java.nio.charset.UnsupportedCharsetException: cp0解决方法 http://blog.youkuaiyun.com/xfei365/article/details/50955731 2.java调用jython报ImportError: No module named…错误解决方原创 2017-10-11 13:25:38 · 744 阅读 · 0 评论 -
python字符集编码
为了保证python读取的不出现乱码,一般在文件的前两行添加两行注释#!/usr/bin/env python3# -*- coding: utf-8 -*-第一行注释是告诉linux系统,windows系统会忽略该行注释第二行注释是告诉python解释器按照UTF-8编码读取源码,否则可能会出现乱码另外如果添加了注释,还出现乱码,可能是由于保存的时候,应该选取以UTF-8无BOM...原创 2019-05-10 10:55:30 · 2219 阅读 · 0 评论 -
字符串里面的 % 是一个普通字符怎么办
字符串里面的 % 是一个普通字符怎么办?这个时候就需要转义,用 %% 来表示一个 % :print('growth rate is %.2f %%' % 4.56)原创 2019-08-30 15:26:14 · 496 阅读 · 0 评论 -
数据转换
df_obj = pd.DataFrame({'data1' : ['a'] * 4 + ['b'] * 4, 'data2' : np.random.randint(0, 4, 8)})df_obj.duplicated()#判断是否重复df_obj.drop_duplicates()df_obj.drop_duplicates('data2')#原创 2017-07-20 09:17:18 · 264 阅读 · 0 评论 -
数据重构
stackdf_obj = pd.DataFrame(np.random.randint(0,10, (5,2)), columns=['data1', 'data2'])stacked = df_obj.stack()# 默认操作内层索引stacked.unstack()# 通过level指定操作索引的级别stacked.unstack(level=0)原创 2017-07-19 20:15:05 · 2282 阅读 · 0 评论 -
python mysql 数据库
python mysql原创 2017-06-28 16:41:30 · 237 阅读 · 0 评论 -
python merge 使用
# coding: utf-8# # 数据连接 merge# In[1]:import pandas as pdimport numpy as np# In[3]:df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'], 'data1' : np.r原创 2017-06-28 16:43:34 · 10841 阅读 · 0 评论 -
python 提取字符串中的数字
利用re模块提取字符串中的数字 [0-9]* 是数字匹配的正则表达式 findlist = re.findall('(\w*[0-9]+)\w*', emp_length_val)原创 2017-06-28 16:44:59 · 14302 阅读 · 0 评论 -
python matplotlib的使用
# coding: utf-8# # matplotlib# In[1]:# 引入matplotlib包import matplotlib.pyplot as pltget_ipython().magic('matplotlib inline')# * figure# In[2]:# 创建figurefig = plt.figure()# * subplot# In[3]:ax1 = fi原创 2017-06-28 17:18:59 · 567 阅读 · 0 评论 -
对连续数值进行指定方式离散化,计算分布,用cut函数
ages = [20,22,25,27,21,23,37,31,61,45,41,32]bins = [18,25,35,60,100]#用的是cut函数cats = pd.cut(ages,bins)print(pd.value_counts(cats))原创 2017-07-13 18:31:37 · 620 阅读 · 0 评论 -
正则表达式拆分字符串
正则表达式拆分字符串原创 2017-07-13 18:29:36 · 6425 阅读 · 7 评论 -
[python]返回每个分组的top_n
def top_n(df, n=3, column='APM'): """ 返回每个分组按 column 的 top n 数据 """ return df.sort_values(by=column, ascending=False)[:n]df_data.groupby('LeagueIndex').apply(top_n)原创 2017-07-19 19:25:58 · 2788 阅读 · 0 评论 -
[python]按key1分组后,计算data1,data2的统计信息并附加到原始表格中
# 按key1分组后,计算data1,data2的统计信息并附加到原始表格中k1_sum = df_obj.groupby('key1').mean().add_prefix('mean_')k1_sum# 方法1,使用mergepd.merge(df_obj, k1_sum, left_on='key1', right_index=True)# 方法2,使用transformk1_sum_原创 2017-07-19 19:31:05 · 1195 阅读 · 0 评论 -
[pandas]数据连接 merge
df_obj1 = pd.DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'], 'data1' : np.random.randint(0,10,7)})df_obj2 = pd.DataFrame({'key': ['a', 'b', 'd'],原创 2017-07-19 19:47:21 · 416 阅读 · 0 评论 -
[python]数据合并concat
Numpy 的concatarr1 = np.random.randint(0, 10, (3, 4))arr2 = np.random.randint(0, 10, (3, 4))np.concatenate([arr1, arr2])np.concatenate([arr1, arr2], axis=1)#按照列Series 的concat# index 没有重复的情况ser_obj1 =原创 2017-07-19 19:57:51 · 6548 阅读 · 0 评论