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()

 

posted on 2018-10-29 20:18  andy_1  阅读(472)  评论(0编辑  收藏  举报

导航