replace() method not working on Pandas DataFrame
You need to assign back
df = df.replace('white', np.nan)
or pass param inplace=True
:
In [50]:
d = {'color' : pd.Series(['white', 'blue', 'orange']),
'second_color': pd.Series(['white', 'black', 'blue']),
'value' : pd.Series([1., 2., 3.])}
df = pd.DataFrame(d)
df.replace('white', np.nan, inplace=True)
df
Out[50]:
color second_color value
0 NaN NaN 1.0
1 blue black 2.0
2 orange blue 3.0
Most pandas ops return a copy and most have param inplace
which is usually defaulted to False
Why is Pandas replace() method not working?
This is bug with dtype='string'
: pandas-dev/pandas #35977 - BUG: replacement works for object but not string dtype
It should work as expected if you load with dtype=str
or dtype='str'
:
df = pd.read_csv(f_name, dtype=str)
df = df[df.columns.dropna()]
df[cols_int] = df[cols_int].replace({"[\$]": "", "[,]": ""}, regex=True)
df[cols_int] = df[cols_int].astype(int)
Why pandas DataFrame replace method does not work (inplace=True argument is used)
Because .replace('=', '')
requires the cell value to be exactly '='
which is obviously not true in your case.
You may instead use it with regex
:
df = pd.DataFrame({'a': ['="abc"', '="bcd"'], 'b': ['="uef"', '="hdd"'], 'c':[1,3]})
df.replace([r'^="', r'"$'], '', regex=True, inplace=True)
print(df)
a b c
0 abc uef 1
1 bcd hdd 3
Two regular expressions are used here, with the first taking care of the head and the second the tail.
Pandas replace function does not work on Series of strings
To work with text data, you need to use '.str' more info here - https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html.
import pandas as pd
dic = {'Text': ['i8am going to school', 'i8am a very good boy']}
df = pd.DataFrame(dic)
d = df['Text'].str.replace('i8am', 'i am')
Output -
0 i am going to school
1 i am a very good boy
Plus, You need to check the function signature. You can't just pass random keyword args and expect it to work.
My Panda's DataFrame Replace Function is not working
Use a dictionary as reference for replace
:
df = pd.DataFrame([['<=50K', '>50K'],
['a', '<=50K']])
# 0 1
# 0 <=50K >50K
# 1 a <=50K
df = df.replace({'<=50K': 0, '>50K': 1})
output:
0 1
0 0 1
1 a 0
If acting on a single columns, use map
:
df[0] = df[0].map({'<=50K': 0, '>50K': 1})
To handle missing values with a default:
d = {'<=50K': 0, '>50K': 1}
df = df.applymap(lambda x: d.get(x, -1))
output:
0 1
0 0 1
1 -1 0
Why the replace method of pandas does not work with None as value?
As was answered here:
df.replace("NAN", None)
is equivalent to df.replace("NAN", None, method='pad')
if you look into the source code.
Both df.replace({"NAN": None})
and df.replace("NAN", np.nan)
give you what you want
Replace method does not work in python pandas
Made some assumptions of your df format but this should work;
import pandas as pd
df = pd.DataFrame({'id': [1, 2, 3], 'date': ['5.30.2021 10:22:00', '5.30.2022 11:45:08', '5.28.2022 10:45:13']})
# replace . with / in date column
df['date'] = df['date'].str.replace('.', '/',regex=False)
print(df)
Output:
id date
0 1 5/30/2021 10:22:00
1 2 5/30/2022 11:45:08
2 3 5/28/2022 10:45:13
Pandas DataFrame replace does not work with inplace=True
You're iterating through the elements within the DataFrame
, in which case I'm assuming it's type str
(or being coerced to str
when you replace
). str.replace
doesn't have an argument for inplace=...
.
You should be doing this instead:
dataset['ver'] = dataset['ver'].str.replace('.', '')
Python Pandas replace() not working
As far as I remember, Python Pandas was changed a little bit in replace. You should try passing over a regex keyword argument.
Like so;
report_df['Owner'].replace({'\r\nName: hwowneremail, dtype: object':''},regex=True)
pandas.DataFrame.replace is not working when I tried to replace '+' from a dataframe
You need to call the str.replace method.
df_raw['Installs'] = df_raw.Installs.str.replace('+', '')
Related Topics
How to Check If a Python Module Exists Without Importing It
Scraping Dynamic Content Using Python-Scrapy
Extract Images from PDF Without Resampling, in Python
What Does 'Valueerror: Cannot Reindex from a Duplicate Axis' Mean
Dynamically Add Field to a Form
Decode Escaped Characters in Url
Repeating Elements of a List N Times
Obtain Active Window Using Python
Read Excel Cell Value and Not the Formula Computing It -Openpyxl
How to Update/Upgrade Pip Itself from Inside My Virtual Environment
Making Python Loggers Output All Messages to Stdout in Addition to Log File
How to Create Test and Train Samples from One Dataframe with Pandas
How to Count the Occurrence of a Certain Item in an Ndarray
How to Find the Location of Python Module Sources
Python String Prints as [U'String']