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、重命名列名
list_17 = temp_17.columns.values.tolist() 取出列名,转为列表
temp_18.set_axis(list_17, axis=’columns’, inplace=True) 用列表命名列
ni.rename(columns={“干扰 0”: “0”,
“干扰1”: “1”,
“干扰2”: “2”,
“干扰3”: “3”,
“干扰4”: “4”}, inplace=True)
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)
Original: https://blog.csdn.net/weixin_43861016/article/details/125744532
Author: weixin_43861016
Title: Python遇到问题记录
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/743067/
转载文章受原作者版权保护。转载请注明原作者出处!