[英]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' ![]() |
Second![]() |
'9.68' ![]() |
Third![]() |
'9.45' ![]() |
Fourth![]() |
'Loan fee:8.10' ![]() |
Fifth![]() |
'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.