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
1
  • the data u shared is not sufficient enuf to try out any suggestions. u could have a look at pandas' clip method though, and see if it helps Commented Apr 6, 2020 at 9:50

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.