.ix indexer works okay for pandas version prior to 0.20.0, but since pandas 0.20.0, the .ix indexer is deprecated, so you should avoid using it. Instead, you can use .loc or iloc indexers. You can solve this problem by:
mask = df.my_channel > 20000
column_name = 'my_channel'
df.loc[mask, column_name] = 0
Or, in one line,
df.loc[df.my_channel > 20000, 'my_channel'] = 0
mask helps you to select the rows in which df.my_channel > 20000 is True, while df.loc[mask, column_name] = 0 sets the value 0 to the selected rows where maskholds in the column which name is column_name.
Update:
In this case, you should use loc because if you use iloc, you will get a NotImplementedError telling you that iLocation based boolean indexing on an integer type is not available.
.ix indexer works okay for pandas version prior to 0.20.0, but since pandas 0.20.0, the .ix indexer is deprecated, so you should avoid using it. Instead, you can use .loc or iloc indexers. You can solve this problem by:
mask = df.my_channel > 20000
column_name = 'my_channel'
df.loc[mask, column_name] = 0
Or, in one line,
df.loc[df.my_channel > 20000, 'my_channel'] = 0
mask helps you to select the rows in which df.my_channel > 20000 is True, while df.loc[mask, column_name] = 0 sets the value 0 to the selected rows where maskholds in the column which name is column_name.
Update:
In this case, you should use loc because if you use iloc, you will get a NotImplementedError telling you that iLocation based boolean indexing on an integer type is not available.
Try
df.loc[df.my_channel > 20000, 'my_channel'] = 0
Note: Since v0.20.0, ix has been deprecated in favour of loc / iloc.
python - replace() method not working on Pandas DataFrame - Stack Overflow
python - replace a value in the entire pandas data frame - Stack Overflow
Pandas: Need to replace only a certain value with another value from a different dataframe.
pandas - How to replace value in a column of dataframe python? - Stack Overflow
Given that this is the top Google result when searching for "Pandas replace is not working" I'd like to also mention that:
replace does full replacement searches, unless you turn on the regex switch. Use regex=True, and it should perform partial replacements as well.
This took me 30 minutes to find out, so hopefully I've saved the next person 30 minutes.
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
I'm losing my mind on this one. I have two dataframes. For simplicity's sake, in df1, let's say there's a column with a project name, and a column with the project owner. Due to a change in systems, some project owners have been replaced with "Default." In df2, I have all the projects and project owners, but only for the "Default" folk. There are, of course, many other columns on both dataframes, but I'm omitting them. Also, the dataframes have completely different column names. I only mention because it's one of the errors I've run into. This is for a script I run every week for work that needs to be changed due to the aforementioned new system.
Example of df1:
| Project Name | Project Owner |
|---|---|
| Project A | Bob Jones |
| Project B | Default |
| Project C | Default |
| Project D | John Roberts |
Example of df2:
| Name of Project | Owner of Project |
|---|---|
| Project B | Bertha Thomas |
| Project C | Jane Smith |
I've tried:
project_dict = dict(zip(df2['Name of Project'], df2['Owner of Project'])) df['Project Owner'] = df['Project Name'].replace(project_dict)
Which outputs:
| Project Name | Project Owner |
|---|---|
| Project A | Project A |
| Project B | Bertha Thomas |
| Project C | Jane Smith |
| Project D | Project D |
I've also tried every way to use loc I could think of, and I'm just lost. The above is the closest I've gotten. Any ideas would be appreciated, and don't hesitate to let me know if I need to post more code.
Thanks!