Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Add new column based on condition on some other column in pandas.

# np.where(condition, value if condition is true, value if condition is false)

df['hasimage'] = np.where(df['photos']!= '[]', True, False)
df.head()
Comment

pandas create new column conditional on other columns

# For creating new column with multiple conditions
conditions = [
    (df['Base Column 1'] == 'A') & (df['Base Column 2'] == 'B'),
    (df['Base Column 3'] == 'C')]
choices = ['Conditional Value 1', 'Conditional Value 2']
df['New Column'] = np.select(conditions, choices, default='Conditional Value 1')
Comment

pandas create a new column based on condition of two columns

conditions = [
    df['gender'].eq('male') & df['pet1'].eq(df['pet2']),
    df['gender'].eq('female') & df['pet1'].isin(['cat', 'dog'])
]

choices = [5,5]

df['points'] = np.select(conditions, choices, default=0)

print(df)
     gender      pet1      pet2  points
0      male       dog       dog       5
1      male       cat       cat       5
2      male       dog       cat       0
3    female       cat  squirrel       5
4    female       dog       dog       5
5    female  squirrel       cat       0
6  squirrel       dog       cat       0
Comment

put two columns together in one column in pandas dataframe

# First dataframe with three columns
dlist=["vx","vy","vz"]
df=pd.DataFrame(columns=dlist)
df["vx"]=df1["v2x"]
df["vy"]=df1["v2y"]
df["vz"]=df1["v2z"]
# second dataframe with three columns
dlist=["vx","vy","vz"]
df0=pd.DataFrame(columns=dlist)
df0["vx"]=df2["v1x"]
df0["vy"]=df2["v1y"]
df0["vz"]=df2["v1z"]
# Here with concat we can create new dataframe with garther both in one
# YOU CAN PUT SOME VALUES IN EACH AND CHECK IT
# WHAT INSIDE THE CONCAT MUST BE A LIST OF DATAFRAME
v = pd.concat([df,df0])
Comment

PREVIOUS NEXT
Code Example
Python :: discord.py get server id 
Python :: how to replace the last character of a string in python 
Python :: python split lines 
Python :: enumerate string pythonm 
Python :: how to save dataframe as csv in python 
Python :: python 1 line for loop with else 
Python :: how to concatenate dataframe in python 
Python :: google-api-python-client python 3 
Python :: transformer un dictionnaire en liste python 
Python :: save turtle programming python 
Python :: get scipy version python 
Python :: python timer 
Python :: int to alphabet letter python 
Python :: django superuser 
Python :: python timer() 
Python :: fetch data from excel in python 
Python :: plotting confusion matrix 
Python :: Python program to print negative numbers in a list 
Python :: how to check for a substring in python 
Python :: python join list ignore none and empty string 
Python :: using a dictionary in python 
Python :: python split string into floats 
Python :: pyautogui tab key 
Python :: python download file from ftp 
Python :: input python 3 
Python :: how to print a string in python 
Python :: how to create a loading in pyqt5 
Python :: conda install pypy 
Python :: null variable in python 
Python :: - inf in python 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =