小编sta*_*tdr的帖子

根据另一列从列中删除子字符串

尝试使用一列中的值(作为字符串)来确定从另一列中删除的内容。列的其余部分必须保持不变。

示例数据:

import pandas as pd

dfTest = pd.DataFrame({
    'date': ['190225', '190225', '190226'],
    'foo': ['190225-file1_190225', '190225-file2_190225', '190226-file3_190226']
})

dfTest
Run Code Online (Sandbox Code Playgroud)

结果数据框:

   |    date   |          foo
------------------------------------
0  |   190225  | 190225-file1_190225
1  |   190225  | 190225-file2_190225
2  |   190226  | 190226-file3_190226
Run Code Online (Sandbox Code Playgroud)

我需要创建 'bar' 列,其中 'foo' 已删除所有 'date' 匹配项。

我要找的是这个:

   |    date   |         foo          |   bar
-----------------------------------------------
0  |   190225  | 190225-file1_190225  | -file1_
1  |   190225  | 190225-file2_190225  | -file2_
2  |   190226  | 190226-file3_190226  | -file3_
Run Code Online (Sandbox Code Playgroud)

'date' 列的内容,无论它们出现在开头、中间还是结尾,都需要为 'foo.' 的每一行删除。 …

python dataframe pandas

6
推荐指数
1
解决办法
2301
查看次数

标签 统计

dataframe ×1

pandas ×1

python ×1