2

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
Asma135
  • 23
  • 4
  • the data u shared is not sufficient enuf to try out any suggestions. u could have a look at pandas' [clip](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.clip.html) method though, and see if it helps – sammywemmy Apr 06 '20 at 09:50

1 Answers1

2

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)
yatu
  • 86,083
  • 12
  • 84
  • 139
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Thanks a lot! I godt my code down to: hrt_data = pd.read_csv(hrtdata_file) cols = hrt_data.select_dtypes(np.number).columns hrt_data[cols] = hrt_data[cols].clip(0, 192) And it is working brilliantly! – Asma135 Apr 06 '20 at 10:27