栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python遇到问题记录

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python遇到问题记录

pandas库使用 1、判断包含特定值

df = cun_lte[cun_lte[‘信息’].str.contains(‘R|A’) == True ]

temp = temp[(temp[‘标识’] == “0”) | (temp[‘标识’] == 0)] # 取 0 的

2、单条件判断(if)

result[‘干扰’] = np.where(result[‘MAX’] > -107, ‘是’, ‘’)

3、多条件判断(嵌套if)

df[‘开关’] = np.where((条件1) & (条件2), ‘是’,‘否’)

4、删除重复值

df = df.drop_duplicates(keep=‘first’) # 删除重复值

5、重命名列名 方法1

list_17 = temp_17.columns.values.tolist() 取出列名,转为列表
temp_18.set_axis(list_17, axis=‘columns’, inplace=True) 用列表命名列

方法2

ni.rename(columns={“干扰 0”: “0”,
“干扰1”: “1”,
“干扰2”: “2”,
“干扰3”: “3”,
“干扰4”: “4”}, inplace=True)

方法3

km.rename(columns={‘名称’: ‘小区’, ‘干扰’:‘KM内干扰’}, inplace=True)

6、组合名字

temp[‘id’] = temp[‘NAME’].map(str) + “_” + temp[‘标识’].map(str) # NAME与ID组合

7、拼接数据(V)

result2 = pd.merge(result1, temp, left_on=‘du_id’, right_on=‘_id’,
how=‘left’, suffixes=[‘’, ‘_SE’])

8、读写csv

temp_o = pd.read_csv(‘xxx.csv’, encoding=‘gbk’, low_memory=False)

mm.to_csv(‘xxx.csv’ , index=False, encoding=‘utf_8_sig’)

9、读写excel

info = pd.read_excel(‘xxx.xlsx’)

info = pd.read_excel(‘xxx.xlsx’, sheetname=‘xxx’)

info.to_excel( ‘project_cn.xlsx’,sheet_name = “L”, index=False)

10、替换

mm.replace([888], [‘’], inplace=True) # 用空值 替换888值

mm[‘干扰’].replace([‘NIL’, ‘’], [-120, -120], inplace = True) # 用-120替换NIL和空值

11、查找

startwith,endwith

mm = [name for name in os.listdir(self.path_result) if name.startswith(‘基本信息’)]

12、按条件删除 单条件

ni.drop(ni[(ni[‘平均’].str.contains(‘NIL’) == True)].index, inplace=True)

多条件

ni.drop(ni[(ni[‘’].str.contains(‘NIL’) == True) &
(ni[‘0’].str.contains(‘NIL’) == True) &
(ni[‘1’].str.contains(‘NIL’) == True) &
(ni[‘2’].str.contains(‘NIL’) == True) &
(ni[‘3’].str.contains(‘NIL’) == True) &
(ni[‘4’].str.contains(‘NIL’) == True) &
(ni[‘全部’].str.contains(‘NIL’) == True)].index, inplace=True)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011236.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号