python-3.x - 数据框在所有行的特定字符串之前拆分

标签 python-3.x pandas dataframe

我有一个数据框 (df),其中包含来自网络抓取练习的 30 000 行

Name     NameID                                                            Age

John     www.link.com/www.link.com/https://www.link.com/ct/John             25
Samanta  www.link.com/www.link.com/https://www.link.com/ct/Samanta          24
Johnny   www.link.com/www.link.com/                                         22
Mary     www.link.com/www.link.com/https://www.link.com/ct/Mary             35

我想以只阅读“https://www.link.com/ct/”部分的方式清理“NameID”行。所以我的输出数据框应该是这样的:

 Name     NameID                                  Age

John     https://www.link.com/ct/John             25
Samanta  https://www.link.com/ct/Samanta          24
Johnny                                            22
Mary     https://www.link.com/ct/Mary             35

到目前为止我的代码:

df['NameID'] = df['NameID'].str.split("https://www.link.com/ct/")[1][1]
df['NameID'] =  "https://www.link.com/ct/" + df['NameID'].astype(str)

现在的输出如下所示:

Name     NameID                                  Age

John     https://www.link.com/ct/John             25
Samanta  https://www.link.com/ct/John             24
Johnny   https://www.link.com/ct/John             22
Mary     https://www.link.com/ct/John             35

有什么帮助吗?

最佳答案

你已经接近了,你需要 .str[1]。尝试将您的代码更改为:

df['NameID'] = df['NameID'].str.split("https://www.link.com/ct/").str[1]
df['NameID'] =  "https://www.link.com/ct/" + df['NameID'].astype(str)

df

      Name                           NameID  Age
0     John     https://www.link.com/ct/John   25
1  Samanta  https://www.link.com/ct/Samanta   24
2   Johnny      https://www.link.com/ct/nan   22
3     Mary     https://www.link.com/ct/Mary   35

您可以稍微调整您的代码以返回 '',正如您在所需结果中指定的那样。

关于python-3.x - 数据框在所有行的特定字符串之前拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66349310/

相关文章:

python - 如何使用python打开触摸键盘?

r - 列出 R 中数据帧中的所有不同字符串

将数据框中的值替换为行名称

Scala Spark 数据框保持前导零

python - 基于列中嵌套的 JSON 添加 DataFrame 列

python - 用测试集中的中位数填充 Nan 值

python - 将日期和时间与列表中的日期时间元素分开

python - 多行 jsons 的 pandas read_json 返回 JSONReader 而不是数据帧

python - 在 Python 中查找至少包含点列表之一的多边形

python - 连接两个 pandas 数据框失败