Python Excel 新增列和对一整列赋值 数据来源于其它列

原文:https://blog.csdn.net/sscc_learning/article/details/76993816 原文:https://www.cnblogs.com/vamei/archive/2013/03/12/2954938.html 原文:https://blog.csdn.net/weixin_36060730/article/details/80380520


1、效果

处理前:

处理后:

2、处理逻辑

入店来源包含手淘搜索时,把搜索的关键词存到列搜索关键字

3、代码如下

import pandas as pd

df = pd.read_excel('test.xlsx')

# 插入列
col_name = df.columns.tolist()
index = col_name.index('入店来源') + 1
col_name.insert(index, '搜索关键字')
df = df.reindex(columns = col_name)

# 修改值
df.loc[df['入店来源'].str.find('手淘搜索') > -1 , '搜索关键字'] = df['入店来源'].str.replace('手淘搜索','')
df.loc[df['入店来源'].str.find('手淘搜索') > -1 , '入店来源'] = '手淘搜索'


# 写入新的Excel中
list_excel = []
list_excel.append(df)

writer = pd.ExcelWriter('test2.xlsx')
pd.concat(list_excel).to_excel(writer,'sheet1',index=False)
writer.save()

print('ok')

猜你喜欢

转载自www.cnblogs.com/guxingy/p/12916940.html