By Mejias
背景:如图所示,下列有一个表格,
在具体的工作中,要求某些列长度一致,未达到指定长度的单元格前面需要加‘0‘直到指定长度。同时要求第二列’gl_name‘的开头都为’gl_‘。
思路:dataFrame的每一个单独的数据都可可以被操作,即可以对其进行字符串操作。
如下所示,我们能够通过字符操作去修改单元格的值。即可以实现向单元格增加字符扩充单元格的长度。
#fill gl
gl = list(df1.loc[:,'GL'])
for i in range(len(gl)):
txt = df1.loc[i,'GL']
if txt[0:2]!='gl':#判断GL列每个单元格开头是不是’gl_’,不是的话加入
df1.loc[i,'GL'] = 'gl' + df1.loc[i,'GL']
从这个思路出发,笔者自定义了两个普遍使用的方法,来实现工作需求。
自定义的方法:
第一个方法ZeroAutoFill(table,column_name,column_length)实现前面填‘0‘直到指定长度:
{Table:dataFrame;column_name:需要填充的列;column_length:需要达到的长度}
第二个方法Fill(table,column_name,content)实现开头非指定内容时增加指定内容:
{Table:dataFrame;column_name:需要填充的列;content:开头要求的内容}
#Function ZeroAutoFill
def ZeroAutoFill(table,column_name,column_length):
listC = list(table.loc[:,column_name])
count = 0
for i in range(len(listC)):
b = "0"
if str(table.loc[i,column_name])!='0':#判断替换空格之前是否为空格
count = column_length - len(str(listC[i]))#只需要对有实际值的asin去做添加“0”
if count > 1:
for j in range(1,count):
b = b + '0'#根据需要达到的字符串长度确定此次需要增加的'0'的个数,如需要长度为2,这里range(1,2)条件就会将b变成'00'
table.loc[i,column_name] = b + str(table.loc[i,column_name])
elif count == 1:#如果还差1个0就达到指定长度,那么不需要改变要增加的字符串b的值,因为b的初值是一个'0'
table.loc[i,column_name] = b + str(table.loc[i,column_name])
else:#如果不差(即已经为指定长度,那么就不需要拼接字符串
continue
#Function Fill
def Fill(table,column_name,content):
listD = list(table.loc[:,column_name])
leftLen = len(content)
for i in range(len(listD)):
txt = str(table.loc[i,column_name])
if txt[0:leftLen]!=content:
table.loc[i,column_name] = content + txt
实现效果:
对excel操作的整体代码:
import pandas as pd
import numpy as np
import os
a = os.getcwd()
os.chdir(r'%s'%a)
filelist = []
list_link=[]
filelist2 = []
for root, dirs,files in os.walk(".",topdown = False):
for name in files:
str1 = os.path.join(root,name)
if str1.split('.')[-1] == 'xlsx':
filelist.append(str1)
for i in filelist:
name = i.split('\\')[1]
filelist2.append(name)
for i in range(len(filelist2)):
list_count = pd.read_excel(r'%s'%filelist2[i],dtype = str)
list_link.append(list_count)
df1 = list_link[0]
df1.fillna(value = '0',inplace=True)#替换空值
df1.to_excel('替换空值后.xlsx')
#Function ZeroAutoFill
def ZeroAutoFill(table,column_name,column_length):
listC = list(table.loc[:,column_name])
count = 0
for i in range(len(listC)):
b = "0"
if str(table.loc[i,column_name])!='0':#判断替换空格之前是否为空格
count = column_length - len(str(listC[i]))#只需要对有实际值的asin去做添加“0”
if count > 1:
for j in range(1,count):
b = b + '0'#根据需要达到的字符串长度确定此次需要增加的'0'的个数,如需要长度为2,这里range(1,2)条件就会将b变成'00'
table.loc[i,column_name] = b + str(table.loc[i,column_name])
elif count == 1:#如果还差1个0就达到指定长度,那么不需要改变要增加的字符串b的值,因为b的初值是一个'0'
table.loc[i,column_name] = b + str(table.loc[i,column_name])
else:#如果不差(即已经为指定长度,那么就不需要拼接字符串
continue
#Function Fill
def Fill(table,column_name,content):
listD = list(table.loc[:,column_name])
leftLen = len(content)
for i in range(len(listD)):
txt = str(table.loc[i,column_name])
if txt[0:leftLen]!=content:
table.loc[i,column_name] = content + txt
#all columns that needs to be updated
columns = ['asin','HTS_code','PCR_code']
length = [10,10,8]
for i in range(len(columns)):
ZeroAutoFill(df1,columns[i],length[i])
Fill(df1,'gl_name','gl_')
df1.to_excel('修改后的表格.xlsx',index =False)