简体   繁体   English

如何将 dataframe 线拆分为多个数据帧?

[英]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.

相关问题 将 dataframe 拆分为多个数据帧 - Split dataframe into multiple dataframes 将pandas数据框拆分为多个数据框 - Split pandas dataframe into multiple dataframes 如何根据标题行将数据帧拆分为多个数据帧 - How to split dataframe into multiple dataframes based on header rows 找到特定的列值后如何将一个数据帧拆分为多个数据帧 - How to split a dataframe in multiple dataframes after a specific column value is found 如何按列值将 Pandas 数据帧拆分/切片为多个数据帧? - How to split/slice a Pandas dataframe into multiple dataframes by column value? 如何在 pandas dataframe 中拆分字符串,并返回多个数据帧 - How to split a string in a pandas dataframe, and return multiple dataframes 如何将具有多种类型信息的 dataframe 拆分为基于字符串的单独数据帧? - How to split dataframe with multiple types of information into separate dataframes based on string? 如何根据列名将数据框拆分为多个数据框 - How to split a dataframe to multiple dataframes bases on column names 如何使用 for 循环将单变量数据帧拆分为多个数据帧 - How to split a univariate dataframe into multiple dataframes using a for loop 如何根据 MultiIndex 的一部分将 DataFrame 拆分为多个 DataFrame? - How to split a DataFrame into multiple DataFrames based off part of a MultiIndex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM