Pandas DataFrame数据的增、删、改、查

Pandas DataFrame数据的增、删、改、查

https://blog.csdn.net/zhangchuang601/article/details/79583551

#删除列

df_2 = df_1.drop(columns=['deptNo','routeNo']).copy()

del df_2['trp_vehicleType']

#列名变更

df_3 = df_2.rename(columns={'dingdanNo':'订单号', 'createTime':'建单时间'})

#列位置调换

df_2.insert(0, 'deptName', df_2.pop('deptName'))  #先删除,再插入。0代表在第一列之前

#dataframe 格式化输出json

outjson = df_2.reset_index(drop=True).to_json(orient='records').encode('utf-8').decode('unicode_escape')
outjson2 = json.dumps(json.loads(outjson), sort_keys=False, indent=2).encode('utf-8').decode('unicode_escape')

#排序

df_1.sort_values(by=['deptName','expSendTime'],inplace=True)

#根据旧列生成新列
df_2['receiverAddress'] = df_2['receiverAddress'].map(lambda x:x.split(',')[0])
df_2['trp_customCode'] = df_2[['trp_customCode','trp_vehicleType']].apply(lambda x:x[0] if x[0]!='' else '',axis=1)

#输出excel

writer = pd.ExcelWriter('output.xlsx')

df1.to_excel(writer,'Sheet1')

df2.to_excel(writer,'Sheet2')

writer.save()

上一篇:文件IO一些注意的地方


下一篇:Could not create pool connection. The DBMS driver exception was: null, message from server: "Host '192.168.XX.XX' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'