Python. Finding thresholds for rows of data

Look into the python package pandas. Here’s a tutorial: https://pandas.pydata.org/pandas-docs/stable/tutorials.html

import pandas as pd

list1 = [-50, -40, -30, -20, -10, 0, 1, 2]
list2 = [-300, -200, -100, 0, 100, 200, 300, 400]

df = pd.DataFrame({'List 1': list1, 'List 2': list2})

newdf = df.copy()
newdf[df > df.median()] = 1
newdf[df < df.median()] = -1

newdf now contains the following:

   List 1  List 2
0      -1      -1
1      -1      -1
2      -1      -1
3      -1      -1
4       1       1
5       1       1
6       1       1
7       1       1

If you want both the new and old lists side by side, you can concatenate the dataframes. It’s also a good idea to rename the columns first:

# rename columns:    
newdf = newdf.rename(columns=lambda x: x + ' after threshold')
# concatenate dataframes:
result = pd.concat([df, newdf], axis=1)

With the following result:

   List 1  List 2  List 1 after threshold  List 2 after threshold
0     -50    -300                      -1                      -1
1     -40    -200                      -1                      -1
2     -30    -100                      -1                      -1
3     -20       0                      -1                      -1
4     -10     100                       1                       1
5       0     200                       1                       1
6       1     300                       1                       1
7       2     400                       1                       1

Browse More Popular Posts

Leave a Comment