-1

I have a data frame as shown in the example below.

        0            1            2    3
0       a       sample            1  NaN
1  sample         this            1  NaN
2    this           is            1  NaN
3  sample         this           is  1.0
4      is  interesting          0.5  NaN
5    this           is  interesting  0.5
6      is            a       sample  1.0
7      is            a          0.5  NaN
8    this           is            a  0.5
9       a       sample         this  1.0

I need to separate data frames based on their number of columns, for example,

df_1
 0       a       sample            1
 1  sample         this            1
 2    this           is            1
 3      is  interesting          0.5 
 4      is            a          0.5

and df_2 
1  sample         this           is  1.0
2    this           is  interesting  0.5
3      is            a       sample  1.0
4    this           is            a  0.5
5       a       sample         this  1.0

These data frames are dynamic. It can have n number of columns. Please suggest some solutions.

Thanks, Aditya.

adikh
  • 306
  • 2
  • 16

1 Answers1

0

I really don't know why someone didn't tried and gave -1 for this question, But I was struggling for it. Finally, I have figured it out.

Considering main dataframe as df_allgrams and it is in list structure.

        d = defaultdict(list)
        from collections import defaultdict
        for i in df_allgrams:
            d[len(i)].append(i)
        out = [d[i] for i in sorted(d)]

for lists in out:
    df_lists = pd.DataFrame(lists)
    print(df_lists)

Gives the actual output shown in above df_1 and df_2.

             0            1    2
0           is            a  0.5
1  interesting            i  1.0
2       sample         this  1.0

             0            1            2    3
0         this           is            a  0.5
1            a       sample         this  1.0
2         this           is  interesting  0.5
3           is  interesting            i  1.0
        0            1            2            3    4
0       a       sample         this           is  1.0
1  sample         this           is  interesting  1.0
2    this           is            a       sample  1.0
3    this           is  interesting            i  1.0

This will also work dynamically.

adikh
  • 306
  • 2
  • 16