[英]Split a Pandas dataframe into multiple dataframes based on the value of a column
[英]Loop Through Pandas Dataframe and split into multiple dataframes based on same column Name
主文件示例:
d = pd.read_excel(path, header = 1,sheet_name='Master File example')
dct={}
for i in d.filter(like= "Customer"):
dct[f'df {i}'] = d.loc[:,i:'TAMBA']
print(dct)
Objective:想从Customer到Customer-1切片dataframe,并创建多个dataframe进行比较。 因此,如果 Customer.1 中存在的任何 col 缺失 Customer 可以为 customer.1 创建相同的 col,并且比较将持续到最后一个客户。
当我们读取具有相同列名的 excel 文件时,它将第二个视为 Customer.1、Customer.2 等等。
结果:仅显示一位客户
# mocking data
df = pd.DataFrame(np.arange(5*6).reshape(5,6), columns=['customer',*'ab']*2)
# grouping horizontally
grouper = df.columns.str.match('.*customer.*', case=False).cumsum()
groups = {f'Frame {i}: {gr.columns[0]}': gr for i, gr in df.groupby(grouper, axis=1)}
for k, v in groups.items():
print(f'Name: {k}')
print(v, '\n')
Output:
Name: Frame 1: customer
customer a b
0 0 1 2
1 6 7 8
2 12 13 14
3 18 19 20
4 24 25 26
Name: Frame 2: customer
customer a b
0 3 4 5
1 9 10 11
2 15 16 17
3 21 22 23
4 27 28 29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.