python pandas 读取excel 和写入 excel

# coding=utf-8
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from _overlapped import NULL
from two import ExcelUtil
from json.decoder import NaN

class PandasUtil(object):
def __init__(self,code,name):
self.code = code
self.name = name

def test(self, excel_file):
series = pd.Series([1,2,3,4],index=['A','B','C','D'])
indexNamesArr1 = series.index.values
indexNamesArr1[0] = 'E'

print(series)
print('index {}\n'.format(series.index))
df = pd.DataFrame({'name':['a','b','c'], 'age':[2,3,4]})
df_x = df.assign(percentage=lambda x: x['age'] * 100)

for index_label, row_series in df.iterrows():
print(index_label)
df.at[index_label , 'age1'] = row_series['age'] * 2

print(df_x)
print(df)
print(df.shape[0])

seriesObj = df.apply(lambda x: True if x['age'] > 2 else False , axis=1)
print(seriesObj)
# Count number of True in series
numOfRows = len(seriesObj[seriesObj == True].index)
print(numOfRows)

# Get ndArray of all column names
columnsNamesArr = df.columns.values
# Modify a Column Name
columnsNamesArr[0] = 'Test'

indexNamesArr = df.index.values
print(indexNamesArr)
indexNamesArr[0] = 2

print(df)


#df1 = df.groupby('userId')['rating'].agg(['count','mean']).reset_index()

df1 = pd.read_excel(excel_file)
# print(df1[['休息食事时间合计','组长姓名', '一次良品数']].groupby('组长姓名').min())
df2 = df1[['休息食事时间合计','组长姓名', '一次良品数']].groupby('组长姓名').agg(['count','mean','sum'])
print(df2)

def cal_huanji(x):
for index_label, row_series in x.iterrows():
x.at[index_label , '换机①'] = cal_r(row_series)

def cal_r(row):
v = 0
if row['自责停产项目号1'] == '换机①' and row['自责停产时间1'] != np.nan:
v = v + row['自责停产时间1']
if row['自责停产项目号2'] == '换机①' and row['自责停产时间2'] != np.nan:
v = v + row['自责停产时间2']
if row['自责停产项目号3'] == '换机①' and row['自责停产时间3'] != np.nan :
v = v + row['自责停产时间3']
if row['自责停产项目号4'] == '换机①' and row['自责停产时间4'] != np.nan :
v = v + row['自责停产时间4']
if row['自责停产项目号5'] == '换机①' and row['自责停产时间5'] != np.nan :
v = v + row['自责停产时间5']
return v

def cal_huanji2(x):
for index_label, row_series in x.iterrows():
x.at[index_label , '换机②'] = cal_r2(row_series)

def cal_r2(row):
v = 0
columns = ['1','2','3','4','5']
for i in columns:
if row['自责停产项目号'+i] == '换机②' and row['自责停产时间'+i] != np.nan:
v = v + row['自责停产时间'+i]
return v


#df1 = df1.assign(换机1=lambda x: cal_huanji(x),换机2=lambda x: cal_huanji2(x))

cal_huanji(df1)
cal_huanji2(df1)

print(df1.query("自责停产项目号1 == '换机①' and 自责停产项目号2== '换机①'"))

print(df1.loc[[36,35], ['系统登记日期','申请状态']])

print(df1[1:5][df1.columns[100:102]])

i = 10
while(i>0):
eval("print(i)")
i = i - 1

#df1[['组长姓名', '一次良品数', '休息食事时间合计']].groupby('组长姓名').sum().plot(kind='bar')
df2.plot(kind='bar')
plt.rcParams['font.sans-serif']=['SimHei']
#plt.show()
plt.savefig('c:/test/a_a_a.png')
df1.to_excel('c:/test/ha_ha.xlsx')

#print(df1.columns);
if __name__ == "__main__":
o = PandasUtil('1','43d')
# o.run()
o.test('c:/test/效率相关.xls')

 

posted @ 2021-07-21 10:31  涵旭  阅读(772)  评论(0编辑  收藏  举报