[英]How to split a dataframe line into a multiple dataframes?
I have a dataframe:我有一个 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:预期 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.标头不是必需的,或者我可以在转换为 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:您可以在列表中指定列名,然后在列表理解中对其进行过滤并将列转换为默认
range
列名DataFrame.set_axis
,通过concat
连接,按DataFrame.sort_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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.