Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列重新排列到表中的下一行_Python_Python 3.x_Pandas - Fatal编程技术网

Python 将列重新排列到表中的下一行

Python 将列重新排列到表中的下一行,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个数据帧,格式如下:- ID A B C D E F W1 a1 b1 c1 d1 e1 f1 W2 a2 b2 c2 d2 e2 f2 W3 a3 b3 c3 d3 e3 f3 W4 a4 b4 c4 d4 e4 f4 W5 a5 b5 c5 d5 e5 f5 W6 a6 b6 c6

我有一个数据帧,格式如下:-

ID   A    B    C     D     E     F
W1   a1   b1   c1    d1    e1    f1
W2   a2   b2   c2    d2    e2    f2
W3   a3   b3   c3    d3    e3    f3
W4   a4   b4   c4    d4    e4    f4
W5   a5   b5   c5    d5    e5    f5
W6   a6   b6   c6    d6    e6    f6
如果我想得到以下格式。使用熊猫

ID   A    B    E     F
W1   a1   b1   e1    f1
W1   c1   d1   e1    f1
W2   a2   b2   e2    f2
W2   c2   d2   e2    f2
W3   a3   b3   e3    f3
W3   c3   d3   e3    f3
W4   a4   b4   e4    f4
W4   c4   d4   e4    f4
W5   a5   b5   e5    f5
W5   c5   d5   e5    f5
W6   a6   b6   e6    f6
W6   c6   d6   e6    f6
然后将此数据帧分解为多个数据帧,以将_写入以列1为文件名的单个csv文件

W1.csv

W1   a1   b1   e1    f1
W1   c1   d1   e1    f1 
W2.csv

W2   a2   b2   e2    f2
W2   c2   d2   e2    f2
W3.csv

W3   a3   b3   e3    f3
W3   c3   d3   e3    f3
W4.csv

W4   a4   b4   e4    f4
W4   c4   d4   e4    f4
W5.csv

W5   a5   b5   e5    f5
W5   c5   d5   e5    f5
W6.csv

W6   a6   b6   e6    f6
W6   c6   d6   e6    f6

如果已确定
C->A
D->B
,则可以重新连接帧,然后
ffill
。通常,
d
只需要将要移动的列与它们应该放置的列关联起来


现在,您可以使用
g
将每个单独的帧写入到您选择的文件中:

for _, group in g:
    print(group, end='\n\n')

将数据帧一分为二后使用熊猫,填充nan并使用for循环生成文件:

df1 = df[['ID', 'A', 'B', 'E', 'F']]
df2 = df[['ID', 'C', 'D']]
df2.columns = ['ID', 'A', 'B']
df = pd.concat([df1, df2], sort = True).sort_values('ID').fillna(method='ffill').reset_index(drop = True)[['ID','A','B','E','F']]
for w in df.ID.unique():
    df[df.ID == w].to_csv(w + '.csv')

是什么规则决定了
c
d
a
b
下?嗨,用户3483203,我对python/pandas非常陌生。你介意分步解释你的解决方案吗?提前谢谢很高兴知道这对你有帮助,@ooikiam。如果你认为我的回答是值得的,我恳请你接受并投赞成票。
   ID   A   B   E   F
0  W1  a1  b1  e1  f1
0  W1  c1  d1  e1  f1

   ID   A   B   E   F
1  W2  a2  b2  e2  f2
1  W2  c2  d2  e2  f2

   ID   A   B   E   F
2  W3  a3  b3  e3  f3
2  W3  c3  d3  e3  f3

   ID   A   B   E   F
3  W4  a4  b4  e4  f4
3  W4  c4  d4  e4  f4

   ID   A   B   E   F
4  W5  a5  b5  e5  f5
4  W5  c5  d5  e5  f5

   ID   A   B   E   F
5  W6  a6  b6  e6  f6
5  W6  c6  d6  e6  f6
df1 = df[['ID', 'A', 'B', 'E', 'F']]
df2 = df[['ID', 'C', 'D']]
df2.columns = ['ID', 'A', 'B']
df = pd.concat([df1, df2], sort = True).sort_values('ID').fillna(method='ffill').reset_index(drop = True)[['ID','A','B','E','F']]
for w in df.ID.unique():
    df[df.ID == w].to_csv(w + '.csv')