python-pandas-使用循环连接列

python-pandas-使用循环连接列,python,pandas,dataframe,zip,concatenation,Python,Pandas,Dataframe,Zip,Concatenation,我有一个需要连接的列列表。一个例子是: import numpy as np cats1=['T_JW', 'T_BE', 'T_FI', 'T_DE', 'T_AP', 'T_KI', 'T_HE'] data=np.array([random.sample(range(0,2)*7,7)]*3) df_=pd.DataFrame(data, columns=cats1) 所以我需要得到每一行的连接(如果可能的话,在每个值之间留一个空格)。我试过: listaFin=['']*1000 f

我有一个需要连接的列列表。一个例子是:

import numpy as np
cats1=['T_JW', 'T_BE', 'T_FI', 'T_DE', 'T_AP', 'T_KI', 'T_HE']
data=np.array([random.sample(range(0,2)*7,7)]*3)

df_=pd.DataFrame(data, columns=cats1)
所以我需要得到每一行的连接(如果可能的话,在每个值之间留一个空格)。我试过:

listaFin=['']*1000
for i in cats1:
    lista=list(df_[i])
    listaFin=zip(listaFin,lista)
但是我得到了一个元组列表:

listaFin:

[((((((('', 0), 0), 1), 0), 1), 0), 1),
 ((((((('', 0), 0), 1), 0), 1), 0), 1),
 ((((((('', 0), 0), 1), 0), 1), 0), 1)]
我需要弄点像这样的东西

[0 0 1 0 1 0 1,
0 0 1 0 1 0 1,
0 0 1 0 1 0 1]
如何仅使用一个或更少的循环(我不想使用双循环)来完成此操作


谢谢。

我认为Python中的空格分隔整数列表不能不包含在字符串中(我可能错了)。话虽如此,我的答案是:

output = []
for i in range(0,df_.shape[0]):
    output.append(' '.join(str(x) for x in list(df_.loc[i])))
print(output)
输出如下所示:
['1 0 0 1 0 1','1 0 0 1 0 1','1 0 0 0 1']

我认为Python中的空格分隔整数列表不能不包含在字符串中(我可能错了)。话虽如此,我的答案是:

output = []
for i in range(0,df_.shape[0]):
    output.append(' '.join(str(x) for x in list(df_.loc[i])))
print(output)
输出如下所示: ['1 0 0 1 0 1','1 0 0 1 0 1','1 0 0 0 1']