具有特定分隔符的pandas.DataFrame 中多行的按列字符串连接

问题描述

是否有一种有效方法来按列连接 DataFrame 的多行的字符串,以便结果是单行,其每列的值是同一列的每个值的连接所有给定的行?

示例

如上所述合并前四行。

>>> 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.ilocDataFrame.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