Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clip just one column of dataframe

Tags:

pandas

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?

like image 607
Philipp Avatar asked Apr 02 '19 07:04

Philipp


People also ask

How do you clip a column in Python?

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.

How do you select one column in a data frame?

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.

How do I strip a column in a data frame?

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.


1 Answers

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)
like image 162
jezrael Avatar answered Nov 02 '22 00:11

jezrael