如何使用变量对数据进行切片,现在 .lookup 已被弃用

How can I use variables to slice my data, now .lookup has been deprecated

提问人:RH4818 提问时间:4/24/2023 最后编辑:DataJanitorRH4818 更新时间:4/25/2023 访问量:46

问:

我有一些使用 .lookup 编写的代码,它从数据表中提取时间戳,然后使用这些时间戳对一些 ECG 数据进行切片。

以前我只是简单地写了

Timestamps = pd.DataFrame(dict(pos=m, val=df.lookup(m, m.index)))
BaselineStart = Timestamps.loc['Baseline_Start_Time']["DateTime"]
BaselineEnd = Timestamps.loc['Baseline_End_Time']['DateTime']

我得到了我的时间戳,我简单地读取了心电图数据,然后使用简单的切片提取了正确的部分。

AstroCol_list =['Timestamp',  'ECG_Value']
Astro_ECG = pd.read_csv('A5_AstroECG.csv', usecols = AstroCol_list)
#changing the timestamp into a useable form for operations ECG Lead 2

#accessing relevant timestamp 
Astro_ECGStart = pd.to_datetime(Astro_ECG.iloc[1]['Timestamp'])

#Changing index of ECGmV so it can be sliced by timestamp, timestamp is 3.90625 ms (changed to match the Astroskin, previous value is EQ)

Astro_ECG.index = pd.date_range(start = Astro_ECGStart, periods=len(Astro_ECG), freq='3.99995ms')
Astro_ECG.index.name = 'Timestamp_ECG

然后,我只需写信即可提取我的数据。

AstroBaseline = Astro_ECG[BaselineStart:BaselineEnd]

但是,现在我必须使用

df = pd.read_csv('AstroskinA20_Edit.csv', usecols = AstroCol_list).fillna(0)
m = df.ne(0).idxmax()

# Reset the index of the DataFrame to ensure that it is sequential
df = df.reset_index()

# Create a new DataFrame with positions and values
melted_df = df.melt(id_vars='index', var_name='pos', value_name='val')

# Filter the rows with the specified positions
Timestamps= melted_df[melted_df['index'].isin(m)]

这在导入数据时效果很好,但现在不再允许我使用切片来选择我的心电图数据集的相关部分。我尝试将 ECG 数据集中的时间戳以及我从数据表中读取的时间戳设置为类,但我仍然收到错误。DateTime

raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: 0   2023-01-19 13:35:59
Name: DateTime, dtype: datetime64[ns]

我知道时代存在,因为它以前有效,但现在不再有效,我不确定如何修复此错误。

Python Pandas 数据帧 切片

评论


答: 暂无答案