Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

python - How to split a DataFrame on each different value in a column?

Below is an example DataFrame.

      0      1     2     3          4
0   0.0  13.00  4.50  30.0   0.0,13.0
1   0.0  13.00  4.75  30.0   0.0,13.0
2   0.0  13.00  5.00  30.0   0.0,13.0
3   0.0  13.00  5.25  30.0   0.0,13.0
4   0.0  13.00  5.50  30.0   0.0,13.0
5   0.0  13.00  5.75   0.0   0.0,13.0
6   0.0  13.00  6.00  30.0   0.0,13.0
7   1.0  13.25  0.00  30.0  0.0,13.25
8   1.0  13.25  0.25   0.0  0.0,13.25
9   1.0  13.25  0.50  30.0  0.0,13.25
10  1.0  13.25  0.75  30.0  0.0,13.25
11  2.0  13.25  1.00  30.0  0.0,13.25
12  2.0  13.25  1.25  30.0  0.0,13.25
13  2.0  13.25  1.50  30.0  0.0,13.25
14  2.0  13.25  1.75  30.0  0.0,13.25
15  2.0  13.25  2.00  30.0  0.0,13.25
16  2.0  13.25  2.25  30.0  0.0,13.25

I want to split this into new dataframes when the row in column 0 changes.

      0      1     2     3          4
0   0.0  13.00  4.50  30.0   0.0,13.0
1   0.0  13.00  4.75  30.0   0.0,13.0
2   0.0  13.00  5.00  30.0   0.0,13.0
3   0.0  13.00  5.25  30.0   0.0,13.0
4   0.0  13.00  5.50  30.0   0.0,13.0
5   0.0  13.00  5.75   0.0   0.0,13.0
6   0.0  13.00  6.00  30.0   0.0,13.0

7   1.0  13.25  0.00  30.0  0.0,13.25
8   1.0  13.25  0.25   0.0  0.0,13.25
9   1.0  13.25  0.50  30.0  0.0,13.25
10  1.0  13.25  0.75  30.0  0.0,13.25

11  2.0  13.25  1.00  30.0  0.0,13.25
12  2.0  13.25  1.25  30.0  0.0,13.25
13  2.0  13.25  1.50  30.0  0.0,13.25
14  2.0  13.25  1.75  30.0  0.0,13.25
15  2.0  13.25  2.00  30.0  0.0,13.25
16  2.0  13.25  2.25  30.0  0.0,13.25

I've tried adapting the following solutions without any luck so far. Split array at value in numpy Split a large pandas dataframe

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you want to groupby the first colum. You could create a dictionary from the groupby object, and have the groupby keys be the dictionary keys:

out = dict(tuple(df.groupby(0)))

Or we could also build a list from the groupby object. This becomes more useful when we only want positional indexing rather than based on the grouping key:

out = [sub_df for _, sub_df in df.groupby(0)]

We could then index the dict based on the grouping key, or the list based on the group's position:

print(out[0])

    0     1     2     3         4
0  0.0  13.0  4.50  30.0  0.0,13.0
1  0.0  13.0  4.75  30.0  0.0,13.0
2  0.0  13.0  5.00  30.0  0.0,13.0
3  0.0  13.0  5.25  30.0  0.0,13.0
4  0.0  13.0  5.50  30.0  0.0,13.0
5  0.0  13.0  5.75   0.0  0.0,13.0
6  0.0  13.0  6.00  30.0  0.0,13.0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...