You need to select that column:
In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df
Out[41]:
Team First Season Total Games
0 Dallas Cowboys 1960 894
1 Chicago Bears 1920 1357
2 Green Bay Packers 1921 1339
3 Miami Dolphins 1966 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 1950 1003
So the syntax here is:
df.loc[<mask>(here mask is generating the labels to index) , <optional column(s)> ]
You can check the docs and also the 10 minutes to pandas which shows the semantics
EDIT
If you want to generate a boolean indicator then you can just use the boolean condition to generate a boolean Series and cast the dtype to int this will convert True and False to 1 and 0 respectively:
In [43]:
df['First Season'] = (df['First Season'] > 1990).astype(int)
df
Out[43]:
Team First Season Total Games
0 Dallas Cowboys 0 894
1 Chicago Bears 0 1357
2 Green Bay Packers 0 1339
3 Miami Dolphins 0 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 0 1003
Answer from EdChum on Stack OverflowYou need to select that column:
In [41]:
df.loc[df['First Season'] > 1990, 'First Season'] = 1
df
Out[41]:
Team First Season Total Games
0 Dallas Cowboys 1960 894
1 Chicago Bears 1920 1357
2 Green Bay Packers 1921 1339
3 Miami Dolphins 1966 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 1950 1003
So the syntax here is:
df.loc[<mask>(here mask is generating the labels to index) , <optional column(s)> ]
You can check the docs and also the 10 minutes to pandas which shows the semantics
EDIT
If you want to generate a boolean indicator then you can just use the boolean condition to generate a boolean Series and cast the dtype to int this will convert True and False to 1 and 0 respectively:
In [43]:
df['First Season'] = (df['First Season'] > 1990).astype(int)
df
Out[43]:
Team First Season Total Games
0 Dallas Cowboys 0 894
1 Chicago Bears 0 1357
2 Green Bay Packers 0 1339
3 Miami Dolphins 0 792
4 Baltimore Ravens 1 326
5 San Franciso 49ers 0 1003
A bit late to the party but still - I prefer using numpy where:
import numpy as np
df['First Season'] = np.where(df['First Season'] > 1990, 1, df['First Season'])
data mining - Pandas change value of a column based another column condition - Data Science Stack Exchange
how to update a pandas dataframe column value, when a specific string appears in another column?
A most efficient way of using conditional replacement in pandas
Pandas: Need to replace only a certain value with another value from a different dataframe.
What I want to achieve: Condition: where column2 == 2 leave to be 2 if column1 < 30 elsif change to 3 if column1 > 90
This can be simplified into where (column2 == 2 and column1 > 90) set column2 to 3. The column1 < 30 part is redundant, since the value of column2 is only going to change from 2 to 3 if column1 > 90.
In the code that you provide, you are using pandas function replace, which operates on the entire Series, as stated in the reference:
Values of the Series are replaced with other values dynamically. This differs from updating with .loc or .iloc, which require you to specify a location to update with some value.
This means that for each iteration of for x in filter1 your code performs global replacement, which is not what you want to do - you want to update the specific row of column2 that corresponds to x from column1 (which you are iterating over).
the problem is 2 does not change to 3 where column1 > 90
This is truly strange. I would expect the code you provided to have changed every instance of 2 in column2 to 3 as soon as it encountered an x >= 30, as dictated by your code conditional statement (the execution of the else branch). This discrepancy may stem from the fact that you are assigning to column2 the result of global replacement performed on the column Output (the contents of which are unknown). In any case, if you want your program to do something under a specific condition, such as x > 90, it should be explicitly stated in the code. You should also note that the statement data['column2'] = data['column2'].replace([2], [2]) achieves nothing, since 2 is being replaced with 2 and the same column is both the source and the destination.
What you could use to solve this particular task is a boolean mask (or the query method). Both are explained in an excellent manner in this question.
Using a boolean mask would be the easiest approach in your case:
mask = (data['column2'] == 2) & (data['column1'] > 90)
data['column2'][mask] = 3
The first line builds a Series of booleans (True/False) that indicate whether the supplied condition is satisfied.
The second line assigns the value 3 to those rows of column2 where the mask is True.
I've had success approaching this in a slightly different way.
import numpy as np
data['column2'] = np.where((data['column1'] < 30)
& (data['column2'] ==2), #Identifies the case to apply to
data['column2'], #This is the value that is inserted
data['column2']) #This is the column that is affected
data['column2'] = np.where((data['column1'] > 90)
& (data['column2'] ==2), #For rows with column1 > 90
data['column3'], #We place column3 values
data['column2']) #In column two
This is a little wordier than a loop, but I've found it to be the most intuitive way to do this sort of data manipulation with pandas.
So, i've figured out how to use the pandas apply method to update/change the values of a column, row-wise based on multiple comparisons like this:
# for each row, if the value of both 'columns to check' are 'SOME STRING', change to 'NEW STRING # otherwise leave it as is my_df ['column_to_change'] = df.apply(lambda row: 'NEW STRING' if row['column_to_check_1'] and row['column_to_check_2'] == 'SOME STRING' else row['column_to_change'], axis=1)
Now, I can't figure out how to expand that beyond simple comparison operators. The specific example I'm trying to solve is:
" for each row, if the string value in COLUMN A contains 'foo', change the value in COLUMN B to 'bar', otherwise leave it as is"
I think this is all right, except the ##parts between the hashmarks##
my_df ['columb_b'] = df.apply(lambda row: 'bar' if ##column A contains 'foo'## else row['columb_b'], axis=1)
An example of code I have to replace based on "No" string:
rolledup_data.loc[rolledup_data['FA to be replaced by sourcing activity']=="No",'PMO status'] = "-" #setting no to nan rolledup_data.loc[rolledup_data['FA to be replaced by sourcing activity']=="No",'Early/On Time/Late?'] = "-"
Logic i want to incorporate for replacement with blanks: If first column contains the string No, then set PMO status // Early/On Time/Late? columns to "-" (blank).
Is there a quicker way to do this then writing same thing line by line where conditional column FA to be replaced by sourcing activity to be replaced is same but replacement columns are different?
| FA to be replaced by sourcing activity | PMO status | Early/On Time/Late? |
|---|---|---|
| No (condition) | In progress (should be blank) | Ear On (should be blank) |
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!