How To Split A Dataframe Line Into A Multiple Dataframes?
I have a dataframe: 0 1 2 3 4 5 6 0 A B C D E F G 1 H I J K L M N 2 O P Q R S T U 3 V W X Y Z I want to split every
Solution 1:
You can specify columns names in list, then in list comprehension filter it and convert columns to default range
columns names by DataFrame.set_axis
, join by concat
, sorting by DataFrame.sort_index
, replace missing values and create default index:
vals = [['2','4','6'], ['0','3'], ['1','5']]
L = [df.loc[:, x].set_axis(range(len(x)), axis=1) for x in vals]
df = pd.concat(L).sort_index(kind='mergesort').fillna('').reset_index(drop=True)
print (df)
0 1 2
0 C E G
1 A D
2 B F
3 J L N
4 H K
5 I M
6 Q S U
7 O R
8 P T
9 X Z
10 V Y
11 W
Post a Comment for "How To Split A Dataframe Line Into A Multiple Dataframes?"