Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas update with condition

import pandas as pd
import numpy as np

df = pd.DataFrame({'value':np.arange(1000000)})

# Solution 1 - Fastest :
df['value'] = np.where(df['value'] > 20000, 0, df['value'])

# Solution 2:
df.loc[df['value'] > 20000, 'value'] = 0

# Solution 3:
df['value'] = df['value'].mask(df['value'] > 20000, 0)

# Solution 4 - Slowest, note that df.where applies where condition is wrong:
df['a'] = df.where(df.a <= 20000, 0)
Comment

pandas replace values in column based on condition

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
Comment

Set value of dataframe using condition

time_sin_grad[time_sin_grad < 0] = 0
Comment

replace pandas column values based on condition

d.loc[d["conditionDisplayName"] == "Brand New", "conditionDisplayName"] = 6
d.loc[d["conditionDisplayName"] != "Brand New", "conditionDisplayName"] = 4
Comment

PREVIOUS NEXT
Code Example
Python :: run py file in another py file 
Python :: print the heat map python 
Python :: JUPYTER CONSUMES 100 disk 
Python :: how to iteratively create a grid within a bigger grid in python 
Python :: selenium find element by link text python 
Python :: knn plot the clusters 
Python :: how to find index of an element in list in python stackoverflow 
Python :: how to limit a long text in djagno 
Python :: discord.py ping command 
Python :: program to split the list between even and odd python 
Python :: python record screen 
Python :: an array of dates python 
Python :: lambda with two columns pandas 
Python :: python min length list of strings 
Python :: f string python not working in linux 
Python :: bnbpay 
Python :: django templateview 
Python :: print underline text python 
Python :: pandas read excel 
Python :: how to plotting points on matplotlib 
Python :: how to cycle through panes in tmux 
Python :: drop null rows pandas 
Python :: how to show multiple image in plt.imshow 
Python :: python json parse 
Python :: only include top ten items django for loop 
Python :: how to change cursor on hover of button in tkinter 
Python :: numpy add axis 
Python :: scikit learn linear regression 
Python :: how to add card using py-trello API 
Python :: fastest way to output text file in python + Cout 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =