根据时间列中两个值之间的差异,将数据帧中的每一行重复 N 次

这里的时间分辨率是 1 秒,我想将其转换为 10 毫秒

https://img.mukewang.com/64e469940001325812530404.jpg

我想将该表中的时间分辨率从 1 秒更改为 10 毫秒,方法是减去每行之间的时间差,将其乘以 100,然后用该数字复制每行。


例如: Row[n] 将被重复 Time((n+1)-n)*100


当时间 = 2 秒(第三行)时,我们有某些值组合将保持不变,直到下一行,时间 = 22 秒(第四行),因此这里的时间差 = 20 秒,基于我想要的(第三行) ) 重复 (20*100)


Row[2] 将重复 (22-2)*100


import pandas

import pandas as pd

# Dataframe from Excel sheet

excel_data_Outputs_df = pandas.read_excel(".xlsx", sheet_name='Outputs')

excel_data_Inputs_df = pandas.read_excel("xlsx", sheet_name='Inputs')

# Exclude All zeros columns

excel_data_Outputs_df = excel_data_Outputs_df.loc[:, (excel_data_Outputs_df != 0).any(axis=0)]

excel_data_Inputs_df = excel_data_Inputs_df.loc[:, (excel_data_Inputs_df != 0).any(axis=0)]


# Get the time difference and convert it 10ms resolution 

shifted=excel_data_Inputs_df.Time.shift(-1)

excel_data_Inputs_df.Time=(shifted-excel_data_Inputs_df.Time)*100

excel_data_Inputs_df['Time'] = excel_data_Inputs_df['Time'].fillna(0)

excel_data_Inputs_df.Time=excel_data_Inputs_df.Time.astype(int)


# Repeat Rows

newexcel_data_Inputs_df = excel_data_Inputs_df.loc[excel_data_Inputs_df.index.repeat(excel_data_Inputs_df.Time)].reset_index(drop=True)

print(newexcel_data_Inputs_df)

print(excel_data_Outputs_df)


汪汪一只猫
浏览 1552回答 1
1回答

繁花不似锦

创建另一列来保存列值的差异,以供重复参考,然后执行如下操作:import pandas as pd# Sample dataframedf = pd.DataFrame({    'id' : ['a', 'b', 'c', 'd'],    'col1' : [4, 5, 6, 7],    'col2' : [3, 2, 4, 3]    })# Create a new column to hold the difference in column values # i.e. the number of times the row repition is required.df['times'] = df.col1 - df.col2# create the finalDf with repeated rowsfinalDf = df.loc[df.index.repeat(df.times)].reset_index(drop=True)print(finalDf.head())语句的输出print如下所示:  id  col1  col2  times0  a     4     3      11  b     5     2      32  b     5     2      33  b     5     2      34  c     6     4      2
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python