Python基础
记录日常知识点
「已注销」
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
pd.concat axis=1出现valueError报错,行数一样合并列
原因:index不同。用df.index发现,两个dataframe一个是RangeIndex, 另一个是int64。 解决方法:# 1. 将int64的index重新设置成rangeindexdf1.reset_index(inplace=True)df1.index# 2. ignore indexdf = pd.concat([df1, df2],ignore_index=True)print(df.index)...原创 2021-09-15 11:53:59 · 756 阅读 · 0 评论 -
奖牌RANK (python pandas)
使用dataframe的 sort_values 多列排序,全设置降序排列import pandas as pdmedal = { "金": [8, 8, 7, 10], "银": [3, 7, 5, 7], "铜": [8, 3, 7, 3], "总数": [19, 18, 19, 20],}df = pd.DataFrame(medal)rank = df.sort_values(by=["金", "银", "铜", "总数"], ascending=.原创 2021-07-27 14:32:22 · 762 阅读 · 0 评论 -
Pycharm 添加内容根 解决导包问题
问题:使用sys.path.append()添加路径后还是无法导包进入preferences 点击项目projects的项目结构,右边添加内容根 添加包目录 ✅原创 2021-03-24 16:30:57 · 703 阅读 · 0 评论 -
pytz 将date确定时区后转换成任意时区
目录0. goal1. pytz2. func3. 实例0. goal将字符串类型的日期时间, 如 "2021-03-1616:05:05" 确认为某一时区时间后,转换成北京时间。(根据个人需求去掉了时间只保留日期)1. pytz可以显示所有国家地区的各个时区,通过这个确定需要的时区pytz.all_timezonesOut[5]: ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', ...原创 2021-03-16 17:29:08 · 363 阅读 · 0 评论 -
Python发送邮件及各种附件
import smtplibfrom email.header import Headerfrom email.mime.application import MIMEApplicationfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextclass SendEmail: def __init__(self): """ 配置发件人信息 .转载 2021-03-03 18:50:52 · 230 阅读 · 1 评论 -
解压zip
importzipfilefile = "文件路径"zip_file = zipfile.ZipFile(file) # 读取zip文件zipfile.is_zipfile(zip_file) # 判断是否为zipprint(zip_file.namelist()) # 获取zip文档内所有文件的名称列表zip_file.extractall('输出的路径') # 解压全部文件...转载 2021-03-03 14:02:59 · 153 阅读 · 0 评论 -
pip->pip3, python2.7->python3.9
1. 由↓引出的问题pip3 install -r requirements.txt2.尝试更新pip解决python -m pip>>> 版本为2.7pip install --upgrade pip>>>Traceback (most recent call last): File "/usr/local/bin/pip", line 5, in <module> from pip._internal...原创 2021-03-02 23:32:05 · 247 阅读 · 0 评论 -
pandas读入excel 数字以00之类的开头
解决方法,直接在读入时变成strpd.read_excel("....", converters={u"以000之类开头的列名" : str})转载 2021-01-08 17:55:35 · 827 阅读 · 0 评论 -
error_bad_lines=False
# 跳过超出header字段的行,如4个字段,579行出现了 5个pd.read_table(, error_bad_lines=False)# 显示的跳过信息b'Skipping line 579: expected 4 fields, saw 5\n'原创 2020-12-25 16:00:44 · 4876 阅读 · 0 评论 -
Role of Underscore (_)
1. interpreter里使用 as a particular variable>>> 5 + 49>>> _ # stores the result of the above expression9>>> _ + 615>>> _15>>> a = _ # assigning the value of _ to another variable>>> a15翻译 2020-12-17 18:16:16 · 132 阅读 · 0 评论 -
替换多个字符
import unicodedatas = 'aac1233xyz'[in]t2 = s.translate({ord('a'): 'X', ord('3'): 'Y'}) print(t2)[out] XXc12YYxyz[in]t3 = s.translate({ord('a'): None, ord('3'): None}) print(t3)[out]c12xyz转载 2020-12-17 11:15:52 · 207 阅读 · 0 评论 -
get column index
1.创建个dfdf = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})2.获取列名df.columns3.获取列的indexdf.columns.get_loc("pear")翻译 2020-12-16 15:27:17 · 492 阅读 · 0 评论 -
**args **kw
参考https://www.jianshu.com/p/0ed914608a2c脚本:https://github.com/WENHUIn/python3.7/blob/main/*arg%26**kw.py转载 2020-12-16 00:19:41 · 101 阅读 · 0 评论 -
TypeError: xxx takes 1 positional argument but 9 were given 【multiprocess】
args=(i)不是tupleargs=(i,)通过添加逗号,形成tupleAdd a comma; tuples are formed by the comma, not the parentheses (although you need parentheses to disambiguate the tuple from other arguments in a call)翻译 2020-12-11 11:43:36 · 1706 阅读 · 0 评论 -
时间戳转时间 & 时间转时间戳
1. 时间戳转时间 【毫秒转】import datetimedef transfer_to_localtime(unix): localtime = datetime.datetime.fromtimestamp(unix / 1000) return localtimeEg:unix = 1605280519000date = transfer_to_localtime(unix)print(date)2020-11-13 23:15:192. 时间转时..转载 2020-12-03 15:09:06 · 527 阅读 · 0 评论 -
transfer脚本 【如何跳过异常值继续执行】【删除unnamed列】【字典遍历】
1.目标:将一串json格式字符串分列 该字符串位于dataframe的data列{"score": 527, "features": {"GD_M_103": 9.0, "GD_M_246": 70.0, "GD_M_238": 0.0, "GD_M_315": 11.0, "GD_M_107": 4.0, "GD_M_319": 6.0, "GD_M_2": 19.0, "GD_M_72": 1.0, "GD_M_60": 0.0, "GD_M_82": 1.0, "GD_M_6": 2.0.原创 2020-11-26 18:07:50 · 199 阅读 · 0 评论 -
convert string to dict
1. 使用 literal_evalfrom ast import literal_evalpython_dict = literal_eval("{'a': 1}")2. 使用 json.loads() 【注意,字符串必须为double quotes】import jsonpython_dict = json.loads('{"a": 1}')转载 2020-11-26 17:38:16 · 136 阅读 · 0 评论 -
dataframe尽量显示行和列
1. 容易卡死#显示所有列pd.set_option('display.max_columns', None)#显示所有行pd.set_option('display.max_rows', None)#设置value的显示长度为100,默认为50pd.set_option('max_colwidth',100)2. 比较好的写法# 设置显示行pd.options.display.max_rows = 300# 设置显示列pd.options.display.max_c.转载 2020-11-26 17:33:14 · 677 阅读 · 0 评论 -
字典get用法
Python 字典(Dictionary) get()方法https://www.runoob.com/python/att-dictionary-get.html语法get()方法语法:dict.get(key, default=None)参数key -- 字典中要查找的键。 default -- 如果指定键的值不存在时,返回该默认值。返回值返回指定键的值,如果键不在字典中返回默认值 None 或者设置的默认值。dict={'Name':'Runoob'...转载 2020-11-17 10:20:39 · 456 阅读 · 0 评论
分享