基于特定ch的pandas数据帧行删除方法

2024-06-02 08:36:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据框里有这个

name : john,
address : Milton Kings,
phone : 43133241

Concern:
customer complaint about the services is so suck 

thank you

如何处理上述操作以仅删除包含:的数据框中的文本行?我的目标是得到只包含以下内容的行。在

^{pr2}$

请帮忙。在


Tags: the数据nameisaddressservicephonecustomer
2条回答

假设你想保留句子的第二部分,你可以使用applymap 解决问题的方法。在

import pandas as pd

#Reproduce the dataframe
l = ["name : john",
"address : Milton Kings",
"phone : 43133241",
"Concern : customer complaint about the services is so suck" ]
df = pd.DataFrame(l)

#split on each element of the dataframe, and keep the second part
df.applymap(lambda x: x.split(":")[1])

输入:

^{pr2}$

输出:

    0
0    john
1    Milton Kings
2    43133241
3    customer complaint about the services is so suck

您可以做的一件事是将“:”后的句子与数据框分开。你可以通过从你的数据帧中创建一个序列来实现这一点。在

假设c是你的系列。在

c=pd.Series(df['column'])
s=[c[i].split(':')[1] for i in range(len(c))]

这样你就能把你的句子和冒号分开。在

相关问题 更多 >