Python Pandas Commands .replace() Not Working despite inplace = True
Question:
I’m trying to use .replace()
on a DataFrame made from .read_excel()
. The command is not working despite using inplace = True, and triple-checking the spelling (copy pasted from original excel doc)
import pandas as pd
def create_frame(path):
df1 = pd.read_excel(path)
return df1.replace(('Visit 2: Day 1','Day 1'), inplace = True)
df = create_frame('example.xlsx')
df.to_csv('test.csv')
i’ve also tried using df2 = df1.replace(('Visit 2: Day 1','Day 1'))
instead of using inplace = True
but the test csv always comes out with ‘Visit 2: Day 1’
I’m having a similar dysfunction (commands not working) with .sort_values
and .reset_index
as well.
Any ideas what i’m doing wrong? All help is appreciated! thank you!
Answers:
You can create the function create_frame
without inplace=True
.
if you use inplace=True
, it will return None
from the func create_frame
.
similarly, Pandas dataframe: set_index with inplace=True returns a NoneType, why??
import pandas as pd
def create_frame(path):
df1 = pd.read_excel(path)
return df1.replace(('Visit 2: Day 1','Day 1'))
df = create_frame('example.xlsx')
df.to_csv('test.csv')
when you use df2 = df1.replace(('Visit 2: Day 1','Day 1'))
, it will not affect df1
.
so without use func, you can do like this:
df1 = pd.read_excel(path)
df2 = df1.replace(('Visit 2: Day 1','Day 1'))
df2.to_csv('test.csv')
df1 = pd.read_excel(path)
df1.replace(('Visit 2: Day 1','Day 1'), inplace=True)
df1.to_csv('test.csv')
I have had the same issue, and Ferris suggestion is the only way it worked for me.
I’m using your suggestion and try to loop it through a folder containing ~100 files.
I’m new to programming and currently writing my thesis. Do you @Ferris have a suggestion on how to loop this function?
I’m trying to use .replace()
on a DataFrame made from .read_excel()
. The command is not working despite using inplace = True, and triple-checking the spelling (copy pasted from original excel doc)
import pandas as pd
def create_frame(path):
df1 = pd.read_excel(path)
return df1.replace(('Visit 2: Day 1','Day 1'), inplace = True)
df = create_frame('example.xlsx')
df.to_csv('test.csv')
i’ve also tried using df2 = df1.replace(('Visit 2: Day 1','Day 1'))
instead of using inplace = True
but the test csv always comes out with ‘Visit 2: Day 1’
I’m having a similar dysfunction (commands not working) with .sort_values
and .reset_index
as well.
Any ideas what i’m doing wrong? All help is appreciated! thank you!
You can create the function create_frame
without inplace=True
.
if you use inplace=True
, it will return None
from the func create_frame
.
similarly, Pandas dataframe: set_index with inplace=True returns a NoneType, why??
import pandas as pd
def create_frame(path):
df1 = pd.read_excel(path)
return df1.replace(('Visit 2: Day 1','Day 1'))
df = create_frame('example.xlsx')
df.to_csv('test.csv')
when you use df2 = df1.replace(('Visit 2: Day 1','Day 1'))
, it will not affect df1
.
so without use func, you can do like this:
df1 = pd.read_excel(path)
df2 = df1.replace(('Visit 2: Day 1','Day 1'))
df2.to_csv('test.csv')
df1 = pd.read_excel(path)
df1.replace(('Visit 2: Day 1','Day 1'), inplace=True)
df1.to_csv('test.csv')
I have had the same issue, and Ferris suggestion is the only way it worked for me.
I’m using your suggestion and try to loop it through a folder containing ~100 files.
I’m new to programming and currently writing my thesis. Do you @Ferris have a suggestion on how to loop this function?