- 用reload(模块名) 可以重载已经import的模块
- str()中不能处理含有unicode的字符,可以处理gbk和utf8
- decode或encode是加参数 errors='ignore’可以忽略该编码不支持的字符
- 查看python 模块所在路径: import module; module.file
- 计算经纬度坐标之间的距离
import geopy.distance as distance
distance.GreatCircleDistance((41.49008, -71.312796), (41.499498, -81.695391)).meters
- pip install xxx --user 是linux根据当前用户目录安装模块
- 在pandas读excel的时候,如果数字前面带0,很有可能会被略去,解决方法。
df = pd.read_excel('excel_name.xlsx',converters = {'col_name':str})
- 直接将string当文件读到dataframe
import io
import pandas as pd
df1 = pd.read_csv(io.StringIO(a.decode('utf8')), header=None, sep='\t')
- 想查看有重复数据时保留所有重复数据
df[df['col_name'].duplicated(keep=False)]
- jupyter在windows上运行配置远程登录
设置密码, jupyter notebook password
在cmd运行,jupyter notebook --port 8899 --ip <IP>
- jupyter 上的任务设置超时关闭
生成配置文件: jupyter notebook --generate-config
在配置文件增加一句,30分钟没有操作关闭kernel:c.MappingKernelManager.cull_idle_timeout = 60*30
- 在已有的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()
- 查看Python内核:conda search “^python$”
- 安装python内核:conda create --name py35 python=3.5
- 激活python环境:source activate py35
- windows安装不了shapely
python包依赖包安装
- rtree 依赖 spatialindex(spatialindex.dll spatialindex_c.dll)
- shapely 依赖 geos(geos.dll geos_c.dll)
在windows安装时把dll文件拷贝到\windows\Systems32即可
- dataframe groupby,将df拆分成df的列表
df_group = [i for _,i in df.groupby('city_code')]
- jupyter notebook 清空输出
from IPython.display import clear_output
clear_output()
- 解决部分编码问题
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