I have a pandas dataframe comprised of 3 columns: from [datetime64], to [datetime64], value [float64]. I just want to clip the "value" column to a maxmimum value.
df = dfo.clip(upper=100)
fails with TypeError: Cannot compare type 'Timestamp' with type 'int'
How can I clip just on column of a dataframe?
Example #2: Use clip() function to clips using specific lower and upper thresholds per column element in the dataframe. Creating a Series to store the lower and upper threshold value for each column element.
Selecting columns based on their name This is the most basic way to select a single column from a dataframe, just put the string name of the column in brackets. Returns a pandas series. Passing a list in the brackets lets you select multiple columns at the same time.
str. strip()” to remove the whitespace from the string. Using strip function we can easily remove extra whitespace from leading and trailing whitespace from starting.
You can specify column:
dfo['value'] = dfo['value'].clip(upper=100)
If possible multiple columns:
cols = ['value', 'another col']
dfo[cols] = dfo[cols].clip(upper=100)
Or if need clip all numeric columns filter them by DataFrame.select_dtypes
:
cols = df.select_dtypes(np.number).columns
dfo[cols] = dfo[cols].clip(upper=100)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With