read_excel_pandas.py
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
df = pd.read_excel('data.xlsx')
print(df)
print(df.groupby(by='name').agg(['mean','sum']))
df['item'].plot(kind='bar')
#plt.show()
plt.savefig('o.pdf')
#print(df.info())
#df.pivot_table(values=['score','grade'], index='item', columns='name', aggfunc=['sum','max']).to_excel('b.xls')
print(df[df['item']>10].filter(['name','grade']))
def test(val):
if val < 10:
return "A"
elif val < 20:
return "B"
else:
return "C"
df['ok'] = df.apply(lambda x: test(x.grade), axis=1)
print(df)
# add columns ways, there have five methods
# df.insert(loc=2, column='c', value=111) insert
# df.reindex(columns=['a','b','c','f'], fill_value='dd') reindex
# df1 = pd.concat([df1, pd.dataFrame(columns=['f])]) concat
# df1.loc[:, 'g'] = 0
# df['x'] = [a,b,c] derectly fill with value