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 line into multiples line in the random condition(it can be any condition):
For example,
df['2'],df['4],df['6]
df['0'],df['3']
df['1'],df['5']
In this case, these three rows should be repeated for every row in the input data frame.
Expected output:
C E G
A D
B F
J L N
H K
I M
Q S U
O R
P T
X Z
V Y
W
#should repeat for other rows too
Headers are not required or I can ignore them while converting to csv.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.