# _*_coding:utf-8 _*_
import pandas as pd
import numpy as np
class baixi:
# 初始化
def __init__(self):
self.pd = pd
self.deal_datas = pd.read_excel('done_.xlsx', sheet_name=None)
# 转置过程,将null 值全部删除,重建。很值得学习
@staticmethod
def squeeze_nan(x):
original_columns = x.index.tolist()
squeezed = x.dropna()
squeezed.index = [original_columns[n] for n in range(squeezed.count())]
return squeezed.reindex(original_columns, fill_value=np.nan)
# 重新获取数据
def get_lc_data(self):
# 获取数据
datas = self.deal_datas
for data in datas.values():
data[data.__eq__(0)] = np.nan
# 调用函数左移数据
data.iloc[:, 9:] = data.iloc[:, 9:].apply(self.squeeze_nan, axis=1)
# 将多个sheet保存至同一个excel中
writer = self.pd.ExcelWriter('result.xlsx')
for i in datas.keys():
datas[i].to_excel(writer,sheet_name = i,index=None)
writer.save()
if __name__ == '__main__':
baixi().get_lc_data()
标签:__,sheet,self,excel,data,pandas,datas
From: https://www.cnblogs.com/tiansz/p/16824889.html