首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用pandas将多行不同的字符串合并为一个字符串

使用pandas将多行不同的字符串合并为一个字符串
EN

Stack Overflow用户
提问于 2021-08-18 14:17:24
回答 2查看 48关注 0票数 0

我有一个有多行的数据帧。有没有什么方法可以把它们组合成一行?标有黄色的。我只想把它们合并成一行。请记住,当您合并时,我希望忽略空行。参见附加图像的输出部分(“Problem.jpg”)。

Problem

我希望我的输出是这样的。

Output

我找不到这个问题的逻辑。有什么想法吗?

已尝试此代码。但它不起作用。

代码语言:javascript
复制
import pandas as pd
all_dfs_1 = pd.read_csv("Test.csv",header=None)
all_dfs_1.groupby(0)[1].apply(' '.join).reset_index()

附加文件:- Test.csv

EN

回答 2

Stack Overflow用户

发布于 2021-08-18 14:30:07

如果将"Drilling good ground all shift“移动到最左侧的列,使文件看起来如下所示:

代码语言:javascript
复制
Drilling good ground all shift
2 x Gyro Surveys
Mixing muds to condition the hole
Driller travelled home for shift change at end of shift

Equipment onsite=

然后我相信您需要结合使用','.join(array)split(',,', ',')来去掉空行,如下所示:

代码语言:javascript
复制
>>> import numpy as np
>>> data = np.loadtxt('Test.csv')
>>> data
array(['Drilling good ground all shift', '2 x Gyro Surveys',
       'Mixing muds to condition the hole',
       'Driller travelled home for shift change at end of shift', '',
       'Equipment onsite='], dtype='<U55')
>>> ','.join(data).replace(',,', ',')
'Drilling good ground all shift,2 x Gyro Surveys,Mixing muds to condition the hole,Driller travelled home for shift change at end of shift,Equipment onsite='

如果您不想手动更改Test.csv,可以使用Pandas执行此操作,将其转换为数组,然后按照上面的步骤进行操作:

代码语言:javascript
复制
>>> import pandas as pd
>>> all_dfs_1 = pd.read_csv(r"Test.csv", header=None)
>>> all_dfs_1
                                                  0                               1   2   3   4   ...  7   8   9   10  11
0                        Comments & Equip. Transfers  Drilling good ground all shift NaN NaN NaN  ... NaN NaN NaN NaN NaN
1                                   2 x Gyro Surveys                             NaN NaN NaN NaN  ... NaN NaN NaN NaN NaN
2                  Mixing muds to condition the hole                             NaN NaN NaN NaN  ... NaN NaN NaN NaN NaN
3  Driller travelled home for shift change at end...                             NaN NaN NaN NaN  ... NaN NaN NaN NaN NaN
4                                                NaN                             NaN NaN NaN NaN  ... NaN NaN NaN NaN NaN
5                                  Equipment onsite=                             NaN NaN NaN NaN  ... NaN NaN NaN NaN NaN

[6 rows x 12 columns]
>>> all_dfs_1.iloc[0, 0] = all_dfs_1.iloc[0, 1]
>>> all_dfs_1[0]
0                       Drilling good ground all shift
1                                     2 x Gyro Surveys
2                    Mixing muds to condition the hole
3    Driller travelled home for shift change at end...
4                                                  NaN
5                                    Equipment onsite=
Name: 0, dtype: object
>>> data = all_dfs_1[0].values
>>> data
array(['Drilling good ground all shift', '2 x Gyro Surveys',
       'Mixing muds to condition the hole',
       'Driller travelled home for shift change at end of shift', '',
       'Equipment onsite='], dtype='<U55')
>>> ','.join(data).replace(',,', ',')
'Drilling good ground all shift,2 x Gyro Surveys,Mixing muds to condition the hole,Driller travelled home for shift change at end of shift,Equipment onsite='
票数 1
EN

Stack Overflow用户

发布于 2021-08-18 14:31:06

不确定这是否是您想要的,它返回一个序列,该序列是数据框中的单个行,所有这些值连接在一起

代码语言:javascript
复制
import pandas as pd
import io

#"reads in" the csv file from a string so it can be tested without the file
all_dfs_1 = pd.read_csv(
    io.StringIO(
"""
Comments & Equip. Transfers
2 x Gyro Surveys
Mixing muds to condition the hole
Driller travelled home for shift change at end of shift

Equipment onsite=
"""
    ), 
    header=None
)

#you'll want to do this instead since you have the file
#all_dfs_1 = pd.read_csv("Test.csv",header=None)


single_row = all_dfs_1.apply(lambda v: ','.join(v))
print(single_row)

输出为

代码语言:javascript
复制
0    Comments & Equip. Transfers,2 x Gyro Surveys,M...
dtype: object

如果你只是想要一个字符串,你也可以这样做:

代码语言:javascript
复制
','.join(all_dfs_1[0].values)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68834138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档