1

I have a column TS which is varying from -60 to 60 and I have a column V which is from -0.1 to 0 to 1 and repeating again and again. I want to add 15 to those values of TS when variable V<0, subtract 15 when V>0. Values in TS and V are of format float64. There are some nan values in TS as well so those can be replaced by zeros what I did here. I tried this but afterward I don't know what to do further.

data = pd.read_table(filename,skiprows=104,decimal=',', sep=';',header=None,names=["V ","TS "]) 
`` 



    import numpy as np   
    TS = data[data.keys()[9]]
    TS = np.array(TS)
    np.nan_to_num(TS)
    TS4 = TS[V<0]+15
    TS5 = TS[V>0]-15
    TS6 = TS[V==0]
    TS7 = TS[np.isnan(TS)] = 0

where


+-------+--------------------+--------------------+------------+
| Index |      TS (kPa)      |      NS(kPa)       | V (mm/min) |
+-------+--------------------+--------------------+------------+
|     0 |          36.052101 |          97.384283 |       -0.1 |
|     1 |          20.240409 |         102.012401 |       -0.1 |
|     2 |          10.491882 |  97.26910600000001 |       -0.1 |
|     3 |           8.717836 |         101.145252 |       -0.1 |
|     4 | 3.7307309999999996 |          99.038843 |       -0.1 |
|     5 |           3.585318 |          99.413168 |       0.1 |
|     6 |             3.6144 |         100.562722 |       0.1 |
|     7 |           2.676773 | 100.13413100000001 |       0.1 |
|     8 |          -4.776546 |          97.868247 |       0.1 |
+-------+--------------------+--------------------+------------+

Expected output


+-------+--------------------+--------------------+------------+
| Index |      TS (kPa)      |      NS(kPa)       | V (mm/min) |
+-------+--------------------+--------------------+------------+
|     0 |          51.052101 |          97.384283 |       -0.1 |
|     1 |          35.240409 |         102.012401 |       -0.1 |
|     2 |          25.491882 |  97.26910600000001 |       -0.1 |
|     3 |           23.717836 |         101.145252 |       -0.1 |
|     4 | 18.7307309999999996 |          99.038843 |       -0.1 |
|     5 |           -12.585318 |          99.413168 |       0.1 |
|     6 |             -12.6144 |         100.562722 |       0.1 |
|     7 |           -14.676773 | 100.13413100000001 |       0.1 |
|     8 |          -19.776546 |          97.868247 |       0.1 |
+-------+--------------------+--------------------+------------+
 

  • 1
    Please, take a while to read [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Your last question went unanswered because of this. – Corralien Jul 06 '21 at 21:35
  • Add input data example and expected output, it will make more clear what you want to achieve. – zswqa Jul 06 '21 at 21:37
  • I have added the expected output and data example. Please have a look. @Corralien Thanks I will take care next time – شاه نواز Jul 06 '21 at 21:54
  • It's really better. +1 :-) Next time, use the output of `print(df.to_string())` to display your dataframe. – Corralien Jul 06 '21 at 22:06

1 Answers1

0

Use np.where and replace NaN from TS after.

Input data:

>>> df
          TS          NS    V
0  36.052101   97.384283 -0.1
1  20.240409  102.012401 -0.1
2  10.491882   97.269106 -0.1
3   8.717836  101.145252 -0.1
4   3.730731   99.038843 -0.1
5   3.585318   99.413168  0.1
6   3.614400  100.562722  0.1
7   2.676773  100.134131  0.1
8  -4.776546   97.868247  0.1
9        NaN   99.313814  0.1
df['TS'] = np.where(df['V'] < 0, df['TS'] + 15, df['TS'] - 15)
df['TS'] = df['TS'].fillna(0)

Output result:

>>> df
          TS          NS    V
0  51.052101   97.384283 -0.1
1  35.240409  102.012401 -0.1
2  25.491882   97.269106 -0.1
3  23.717836  101.145252 -0.1
4  18.730731   99.038843 -0.1
5 -11.414682   99.413168  0.1
6 -11.385600  100.562722  0.1
7 -12.323227  100.134131  0.1
8 -19.776546   97.868247  0.1
0   0.000000   99.313814  0.1
Corralien
  • 109,409
  • 8
  • 28
  • 52