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 for循环中的Concat列_Python_Python 3.x_Pandas_For Loop_Concat - Fatal编程技术网

Python for循环中的Concat列

Python for循环中的Concat列,python,python-3.x,pandas,for-loop,concat,Python,Python 3.x,Pandas,For Loop,Concat,我有以下数据集: 1 8 15 22 2 9 16 23 3 10 17 24 4 11 18 25 5 12 19 26 6 13 20 27 7 14 21 28 我希望得到以下结果: 1 2 3 4 5 6 7 8 ... 23 24 25 26 27 28 所以我想循环我的数据集的所有列,并将每一列连接到第一列 import pandas as pd df = pd.read_csv("data.csv", delimit

我有以下数据集:

1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28
我希望得到以下结果:

1
2
3
4
5
6
7
8
...
23
24
25
26
27
28
所以我想循环我的数据集的所有列,并将每一列连接到第一列

import pandas as pd

df = pd.read_csv("data.csv", delimiter=";", header=-1)

number_of_columns= len(df.columns)
print(number_of_columns)


for i in range (1,number_of_columns):
  df1 = df.iloc[:,i]
  df2 = pd.concat([df,df1], ignore_index=True)


print(df2)
这样,只有最后一列连接到最终数据帧中。我知道df2在for循环的每次迭代中都会被覆盖

那么,如何在每个for循环之后“保存”df2,以便每个列都是连接的呢

非常感谢

用于列方向

stack
+
tolist
按行排列

melt
unstack
+
tolist
按列计算

stack
+
tolist
按行排列

melt
unstack
+
tolist
您也可以这样做:

txt = '''1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28'''
arr1 = np.fromstring(txt, dtype=int, sep=' ')
arr1.reshape(7,-1).flatten(order = 'F') # for column wise, 'C' can be used for row wise.
您也可以这样做:

txt = '''1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28'''
arr1 = np.fromstring(txt, dtype=int, sep=' ')
arr1.reshape(7,-1).flatten(order = 'F') # for column wise, 'C' can be used for row wise.

您可能不需要第三方库。您可以使用标准库中的
csv
itertools
模块返回数字列表:

from io import StringIO
from itertools import chain
import csv

mystr = StringIO("""1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28""")

with mystr as fin:
    reader = csv.reader(mystr, skipinitialspace=True, delimiter=' ')
    res = list(map(int, chain.from_iterable(zip(*reader))))

print(res)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
 21, 22, 23, 24, 25, 26, 27, 28]

您可能不需要第三方库。您可以使用标准库中的
csv
itertools
模块返回数字列表:

from io import StringIO
from itertools import chain
import csv

mystr = StringIO("""1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28""")

with mystr as fin:
    reader = csv.reader(mystr, skipinitialspace=True, delimiter=' ')
    res = list(map(int, chain.from_iterable(zip(*reader))))

print(res)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
 21, 22, 23, 24, 25, 26, 27, 28]
简单地说

简单地说

使用
df.unstack()
使用
df.unstack()
from io import StringIO
from itertools import chain
import csv

mystr = StringIO("""1   8   15  22
2   9   16  23
3   10  17  24
4   11  18  25
5   12  19  26
6   13  20  27
7   14  21  28""")

with mystr as fin:
    reader = csv.reader(mystr, skipinitialspace=True, delimiter=' ')
    res = list(map(int, chain.from_iterable(zip(*reader))))

print(res)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
 21, 22, 23, 24, 25, 26, 27, 28]
 pd.Series(df.values.flatten())
 (or)
 pd.Series(df.unstack().values)