简体   繁体   English

为 pandas dataframe 的每一行替换列中的字符串

[英]Replace a string in a column for each row of a pandas dataframe

Edit: I had to add this part to my original question as it's relevant.编辑:我不得不将此部分添加到我原来的问题中,因为它是相关的。

I have a frame which contains many different prefixes in the name column called dfload .我有一个框架,它在名为dfload的名称列中包含许多不同的前缀。

I use the following command to create a slice called df .我使用以下命令创建一个名为df的切片。

df = dfload.loc[dfload['Name'].str.contains("testData/")]

Original Question continues from here:原始问题从这里继续:

Then, I have the following pandas dataframe called df ,然后,我有以下 pandas dataframe 称为df

   name               etc etc etc
0  testData/example1  etc ...
1  testData/example2  ...
2  testData/example3
3  testData/example4
...

I want to replace the string testData/ with nothing for the entire column so it looks like this我想用整个列的任何内容替换字符串testData/所以它看起来像这样

   name      etc etc etc
0  example1  etc ...
1  example2  ...
2  example3
3  example4
...

I used the following command df['name'] = df['name'].str.replace('testData/','') .我使用了以下命令df['name'] = df['name'].str.replace('testData/','')

But I get this error,但我得到这个错误,

<ipython-input-20-dae746394d2d>:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df['name'] = df['name'].str.replace('testData/','')

The dataframe looks fine, why am I getting this error? dataframe 看起来不错,为什么会出现此错误? What's the "proper" way to do this?这样做的“正确”方法是什么?

to avoid the warning that you're getting, create df like this:为避免您收到警告,请像这样创建df

import pandas as pd
df = pd.DataFrame(dfload[dfload.name.str.contains('testdata/')])

specifying that it's a dataframe and not a slice is probably what keeps pandas from throwing the warning指定它是 dataframe 而不是切片可能是阻止 pandas 抛出警告的原因

Use this:用这个:

df.name = df.name.str.replace('testData/','',regex = True)

You should try using lamda funtion to apply the replace statement over every row:您应该尝试使用 lamda 函数在每一行上应用替换语句:

df["name"]= df.apply(lambda x: x['name'].replace('testData/',''), axis=1)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM