简体   繁体   中英

Pandas Dataframe - Get index values based on condition

I have a text file called data.txt containing tabular data look like this:

                        PERIOD
CHANNELS    1      2      3      4       5 
0         1.51   1.61   1.94   2.13   1.95 
5         1.76   1.91   2.29   2.54   2.38 
6         2.02   2.22   2.64   2.96   2.81 
7         2.27   2.52   2.99   3.37   3.24 
8         2.53   2.83   3.35   3.79   3.67 
9         2.78   3.13   3.70   4.21   4.09 
10        3.04   3.44   4.05   4.63   4.53

In the CHANNELS column are the channel numbers of an instrument and in the other 5 columns are the maximum energy that that particular channel can detect in periods 1, 2, 3, 4 and 5 respectively.

I want to write a python code which gets the inputs: Period, Lower energy and Higher energy from the user and then gives out the channel numbers corresponding to the Lower energy and Higher energy for a given period.

For example:

Enter the period:
>>1
Enter the Lower energy:
>1.0
Enter the Higher energy:
>2.0
#Output
The lower energy channel is 0
The higher energy channel is 6

This is what I have written so far:

import numpy as np
import pandas as pd

period = int(input('Enter the period: '))
lower_energy = float(input('Enter the lower energy value: '))
higher_energy = float(input('Enter the higher energy value: '))
row_names = [0, 5, 6, 7, 8, 9, 10]
column_names = [1, 2, 3, 4, 5] 
data_list = []
with open('data.txt') as f:
lines = f.readlines()[2:]
for line in lines:
    arr = [float(num) for num in line.split()[1:]]
    data_list.append(arr)
df = pd.DataFrame(data_list, columns=column_names, index=row_names)
print (df, '\n')
print (df[period])

Help me add to this.

You can add the following code:

Retrieve the index based on the condition. Assumes constant increasing down the channels.

lower_channel_energy = df[df[period]>lower_energy].index[0]
high_channel_energy =  df[(df[period]<higher_energy).shift(-1)==False].index[0]

Printing the channels that we calculated:

print("The lower energy channel is {}".format(lower_channel_energy))
print("The higher energy channel is {}".format(high_channel_energy))

This solution assumes that the energy is increasing on the channels going down.

You can actually read your file directly with Pandas to simplify the program. I can reproduce the output you are expecting with:

import pandas as pd

df = pd.read_csv('data.txt', engine='python' header=1,sep=r'\s{2,}')

period = input('Enter the period: ')
lower_energy = float(input('Enter the lower energy value: '))
higher_energy = float(input('Enter the higher energy value: '))

# select the channels within the ranges provided
lo_e_range = (df[period] > lower_energy)
hi_e_range = (df[period] > higher_energy)

# Indices of the lower and higher energy channels
lec = df[period][lo_e_range].index[0]
hec = df[period][hi_e_range].index[0]

print('The lower energy channel is {}'.format(df['CHANNELS'][lec]))
print('The higher energy channel is {}'.format(df['CHANNELS'][hec]))

I have edited the code to take into account your comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM