python 一些小注意事项

  1. 用reload(模块名) 可以重载已经import的模块
  2. str()中不能处理含有unicode的字符,可以处理gbk和utf8
  3. decode或encode是加参数 errors='ignore’可以忽略该编码不支持的字符
  4. 查看python 模块所在路径: import module; module.file
  5. 计算经纬度坐标之间的距离
import geopy.distance  as distance
distance.GreatCircleDistance((41.49008, -71.312796), (41.499498, -81.695391)).meters
  1. pip install xxx --user 是linux根据当前用户目录安装模块
  2. 在pandas读excel的时候,如果数字前面带0,很有可能会被略去,解决方法。
df = pd.read_excel('excel_name.xlsx',converters = {'col_name':str})
  1. 直接将string当文件读到dataframe
import io
import pandas as pd
df1 = pd.read_csv(io.StringIO(a.decode('utf8')), header=None, sep='\t')
  1. 想查看有重复数据时保留所有重复数据
df[df['col_name'].duplicated(keep=False)]
  1. jupyter在windows上运行配置远程登录
设置密码, jupyter notebook password
在cmd运行,jupyter notebook --port 8899 --ip <IP>
  1. jupyter 上的任务设置超时关闭
生成配置文件: jupyter notebook --generate-config
在配置文件增加一句,30分钟没有操作关闭kernel:c.MappingKernelManager.cull_idle_timeout = 60*30
  1. 在已有的excel上加sheet
import pandas
from openpyxl import load_workbook

book = load_workbook('file_name.xlsx')
writer = pandas.ExcelWriter('file_name.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

df2.to_excel(writer, "sheet_name", index=False)

writer.save()
  1. 查看Python内核:conda search “^python$”
  2. 安装python内核:conda create --name py35 python=3.5
  3. 激活python环境:source activate py35
  4. windows安装不了shapely
python包依赖包安装
- rtree 依赖 spatialindex(spatialindex.dll   spatialindex_c.dll)
- shapely 依赖 geos(geos.dll  geos_c.dll)
在windows安装时把dll文件拷贝到\windows\Systems32即可
  1. dataframe groupby,将df拆分成df的列表
df_group = [i for _,i in df.groupby('city_code')]
  1. jupyter notebook 清空输出
from IPython.display import clear_output
clear_output()
  1. 解决部分编码问题
import sys  
stderr = sys.stderr
stdin = sys.stdin
stdout = sys.stdout
reload(sys)  
sys.setdefaultencoding('utf8')
sys.stderr = stderr 
sys.stdin = stdin 
sys.stdout = stdout
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值