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)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值