根据行是否包含字符串将数据框拆分为多个数据框

时间:2018-12-06 22:15:47

标签: python pandas

我想基于一行是否包含某个字符串“ Customer”和所有后续行来拆分DataFrame,直到另一行包含“ Customer”,即

c_name       |   cost   | p_date
Customer: xx |          | 
hat          |    12    |   1/1/2018
bat          |    13    |   2/3/2018
Customer: xy |          |
mat          |    14    |   1/2/2018

到      DF1

c_name       |   cost   | p_date
Customer: xx |          | 
hat          |    12    |   1/1/2018
bat          |    13    |   2/3/2018

DF2

c_name       |   cost   | p_date
Customer: xy |          |
mat          |    14    |   1/2/2018

我知道我可以在哪里基于df.c_name.str.contains('Customer')拆分DataFrame,但是如何使用它拆分DataFrame?

1 个答案:

答案 0 :(得分:2)

str.contains + groupby

m = df.c_name.str.contains('Customer').cumsum()
d = {f'df{i}': g for i, g in df.groupby(m)}

for k, v in d.items():
    print(k)
    print(v, end='\n\n')

df1
         c_name  cost    p_date
0  Customer: xx   NaN       NaN
1           hat  12.0  1/1/2018
2           bat  13.0  2/3/2018

df2
         c_name  cost    p_date
3  Customer: xy   NaN       NaN
4           mat  14.0  1/2/2018

要访问各个DataFrame,只需使用d['df1']等...