[英]Pandas Dataframe - Get index values based on condition
我有一個名為data.txt的文本文件,其中包含表格數據,如下所示:
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
在“通道”列中是儀器的通道號,在其他5列中是該特定通道分別在周期1、2、3、4和5中可以檢測到的最大能量。
我想編寫一個python代碼,從用戶那里獲取以下輸入:周期,較低能量和較高能量,然后給出給定時間段內對應於較低能量和較高能量的通道號。
例如:
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
到目前為止,這是我寫的:
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])
幫我補充一下。
您可以添加以下代碼:
根據條件檢索索引。 假設不斷增加通道。
lower_channel_energy = df[df[period]>lower_energy].index[0]
high_channel_energy = df[(df[period]<higher_energy).shift(-1)==False].index[0]
打印我們計算出的通道:
print("The lower energy channel is {}".format(lower_channel_energy))
print("The higher energy channel is {}".format(high_channel_energy))
該解決方案假定在下降的通道上能量正在增加。
實際上,您可以直接使用Pandas讀取文件以簡化程序。 我可以復制您期望的輸出:
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]))
我已編輯代碼以考慮您的評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.