Reputation: 23
I have a data set of 7 columns and 2557 rows in a pandas dataframe.
Date Inlet 1 Inlet 2 Inlet 3 Inlet 4 Inlet 5 Inlet 6
01-01-13 0.40 0.41 0.36 10.39 0.37 0.47
02-01-13 -15 0.56 71.90 250.98 90.67 40.89
...
I am trying to replace all negative values with 0 and all values above 192 with 192. I have succeeded in this, but the new dataframe I get is lacking the first row (date). I guess it is left out, because it isn't considered a numeric value? How do I get a new dataframe with the corrected data and still keeping the date column?
I've tried out answers from this question: How to replace negative numbers in Pandas Data Frame by zero
And written following code:
hrt_data = pd.read_csv(hrtdata_file)
num_hrt = hrt_data._get_numeric_data()
num_hrt[num_hrt < 0] = 0
num_hrt[num_hrt > 192] = 192
Upvotes: 2
Views: 97
Reputation: 862406
Use DataFrame.select_dtypes
for only numeric data and then use DataFrame.clip
with assign back only to numeric columns:
print (df)
Date Inlet 1 Inlet 2 Inlet 3 Inlet 4 Inlet 5 col
0 01-01-13 -0.4 500.41 0.36 0.39 0.37 string1
df1 = df.select_dtypes(np.number)
df[df1.columns] = df1.clip(0, 192)
print (df)
Date Inlet 1 Inlet 2 Inlet 3 Inlet 4 Inlet 5 col
0 01-01-13 0.0 192.0 0.36 0.39 0.37 string1
Solution with extract numeric columns names, thank you, @yatu:
cols = df.select_dtypes(np.number).columns
df[cols] = df[cols].clip(0, 192)
Upvotes: 2