简体   繁体   English

如何使用 Python 删除列中单元格的某些部分?

[英]How do I remove certain parts of cells in a column using Python?

So I have a column of strings of numbers in which certain cells have words ahead of the strings.所以我有一列数字字符串,其中某些单元格在字符串前面有单词。 It looks a little something like this:它看起来有点像这样:

Names姓名 Values价值观
First第一的 '9.90' '9.90'
Second第二 '9.68' '9.68'
Third第三 '9.45' '9.45'
Fourth第四 'Loan fee:8.10' '贷款费用:8.10'
Fifth第五 '9.98' '9.98'

Now I've tried a lot of different ideas just to get the 'Loan fee:' removed, basically i first converted it into a list called newz and then tried现在我尝试了很多不同的想法只是为了去掉“贷款费用:”,基本上我首先将它转换成一个名为 newz 的列表,然后尝试

e=[]
for i in newz:
    i.replace('Loan fee:','')
    e.append(i)

Tried using regex as well:也尝试使用正则表达式:

def change(i):
    re.sub('Loan fee:','',i)

result = list(map(lambda x: change(x),newz))

So far nothing's worked到目前为止没有任何效果

If you're using Pandas:如果您使用熊猫:

import pandas as pd

df = pd.DataFrame({
    'Names': ['First', 'Second', 'Third', 'Fourth', 'Fifth'],
    'Values': ['9.90', '9.68', '9.45', 'Loan fee:8.10', '9.98']
})

df['Values'] = df['Values'].str.replace('Loan fee:', '')
print(df)

Outputs输出

    Names Values
0   First   9.90
1  Second   9.68
2   Third   9.45
3  Fourth   8.10
4   Fifth   9.98

str.replace returns a new string. str.replace返回一个新字符串。 Also you should first check whether the string contains 'Loan fee:' and then replace it.此外,您应该首先检查字符串是否包含 'Loan fee:' 然后替换它。

So you should do:所以你应该这样做:

e=[]
for i in newz:
    if "Loan fee:" in i:
        s = i.replace('Loan fee:','')
        e.append(s)
    else:
        e.append(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中遍历文本文件的某些部分? - How do I loop through certain parts of a text file in Python? 如何在Python中提取字符串的某些部分? - How do I extract certain parts of strings in Python? python - 如何用python的re省略字符串的某些部分? - How do I omit certain parts of a string with python's re? 如何在 Python 中使用 xlsxwriter 创建“if and”语句以有条件地格式化单元格列 - How do I create an “if and” statement to conditionally format a column of cells using xlsxwriter in Python 仅当使用 Python 和 Faker 的上一列中存在某个值时,如何将数据添加到列中? - How do I add data to a column only if a certain value exists in previous column using Python and Faker? 如何在提取后使用python删除特定的列 - how to remove a certain column after extraction, using python 如何从python中的图像中删除某些文本? - How do I remove certain text from an image in python? 如何删除 Python OpenCV 中图像的某些部分? - How do I remove certain part of an Image in Python OpenCV? 如何从 python 的列表中删除某些元素? - How do I remove certain elements from a list in python? 如何从 Python 中的嵌套字典中删除某些键? - How do I remove certain keys from a nested dictionary in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM