What do you want to save?
Add Code snippet
New code examples
-
Python 2022-02-13 15:30:05
How to replace both the diagonals of dataframe with 0 in pandas
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(1,100, 100).reshape(10, -1)) out = df.where(df.values != np.diag(df),0,df.where(df.values != np.flipud(df).diagonal(0),0,inplace=True)) Add solution -
Python 2021-11-02 04:10:12
Filters rows using the given condition
# Filters rows using the given condition df.filter(df.age > 3).collect() # [Row(age=5, name='Bob')] df.where(df.age == 2).collect() # Row(age=2, name='Alice')] df.filter("age > 3").collect() # [Row(age=5, name='Bob')] df.where("age ... Add solution -
Python 2021-09-13 00:06:03
pandas where
# replaces values with other where condition is False DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False) import pandas as pd df = pd.DataFrame({'values':[1,2,3,4]}) df.where(df['values'] % 2 == 0, -1)... Add solution -
Python 2021-09-10 08:33:03
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['... Add solution
Best helpers
Ranking is empty