笔记(遍历文件夹,合并数据)

import pandas as pd
import numpy as np
import glob


all_data = pd.DataFrame()
for f in glob.glob("../data/sales-*-2014.xlsx"):
    df = pd.read_excel(f)
    all_data = all_data.append(df,ignore_index=True)

all_data['date'] = pd.to_datetime(all_data['date'])

status = pd.read_excel("../data/customer-status.xlsx")
all_data_st = pd.merge(all_data, status, how='left')

all_data_st[all_data_st["account number"]==737550]
all_data_st['status'].fillna('bronze',inplace=True)

all_data_st["status"] = all_data_st["status"].astype("category")
all_data_st.dtypes
all_data_st.sort_values(by=["status"])
all_data_st["status"].cat.set_categories([ "gold","silver","bronze"],inplace=True)
all_data_st["status"].describe()
all_data_st.groupby(["status"])["quantity","unit price","ext price"].mean()
all_data_st.groupby(["status"])["quantity","unit price","ext price"].agg([np.sum,np.mean, np.std])

all_data_st.drop_duplicates(subset=["account number","name"]).iloc[:,[0,1,7]].groupby(["status"])["name"].count()

"""
https://github.com/chris1610/pbpython/blob/master/notebooks/Combining-Multiple-Excel-File-with-Pandas.ipynb
"""

你可能感兴趣的:(笔记(遍历文件夹,合并数据))