Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

make a condition statement on column pandas

df['color'] = ['red' if x == 'Z' else 'green' for x in df['Set']]
Comment

make a condition statement on column pandas

df.loc[df['column name'] condition, 'new column name'] = 'value if condition is met'
Comment

if condition dataframe python

df.loc[df['age1'] - df['age2'] > 0, 'diff'] = df['age1'] - df['age2']
Comment

Python | Creating a Pandas dataframe column based on a given condition

# importing pandas as pd
import pandas as pd
  
# Creating the dataframe
df = pd.DataFrame({'Date' : ['11/8/2011', '11/9/2011', '11/10/2011',
                                        '11/11/2011', '11/12/2011'],
                'Event' : ['Music', 'Poetry', 'Music', 'Music', 'Poetry']})
  
# Print the dataframe
print(df)
Comment

PREVIOUS NEXT
Code Example
Python :: settings.debug django 
Python :: how to make a clock in python 3 
Python :: python count how many times a character appears in a string 
Python :: Setting up Colab for Kaggle Downloads 
Python :: fibonacci sequence in python using whileloop 
Python :: itertools .cycle() 
Python :: django meta attributes 
Python :: export some columns to csv pandas 
Python :: create a virtualenv python3 
Python :: python dict remove duplicates where items are not the same 
Python :: matplotlive y axis 
Python :: doomsday fuel foobar 
Python :: create new python environment check 
Python :: live server python 
Python :: nonlocal keyword python 
Python :: Filter with List Comprehension 
Python :: neural network hyperparameter tuning 
Python :: python get env 
Python :: static files not loading 404 error django 
Python :: estimate time to run a chunk of code in python 
Python :: python pandas series to title case 
Python :: if condition dataframe python 
Python :: range python 
Python :: python if statement 
Python :: Python Frozenset operations 
Python :: create django project 
Python :: pandas filter rows by value 
Python :: create a conda environment 
Python :: vscode python multiline comment 
Python :: python to make video 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =