是否有有效的方式连接字符串的多行数据帧的列明智,这样的结果是单行,其每列的值是所有给定行的同一列的每个值的连接?
按照上面的解释合并前四行。
>>> df = pd.DataFrame([["this", "this"], ["is", "is"], ["a", "a"], ["test", "test"], ["ignore", "ignore"]])
>>> df
0 1
0 this this
1 is is
2 a a
3 test test
4 ignore ignore
均接受结果:
0 1
0 this is a test this is a test
0
1 this is a test
2 this is a test
如果需要将没有最后使用DataFrame.iloc
的所有行与DataFrame.agg
连接:
s = df.iloc[:-1].agg(' '.join)
print (s)
0 this is a test
1 this is a test
dtype: object
一行DataFrame
加上Series.to_frame
的转置:
df = df.iloc[:-1].agg(' '.join).to_frame().T
print (df)
0 1
0 this is a test this is a test
对于所有行:
s = df.agg(' '.join)
print (s)
0 this is a test ignore
1 this is a test ignore
dtype: object
df = df.agg(' '.join).to_frame().T
print (df)
0 1
0 this is a test ignore this is a test ignore