0

有没有一种有效的方法来连接 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
4

1 回答 1

1

如果需要加入所有行而不最后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
于 2021-03-11T13:40:11.497 回答