简体   繁体   English

根据Time Column中两个值之间的差异,将Dataframe中的每一行重复N次

[英]Repeat each Row in a Dataframe different N times according to the difference between two value in the Time Column

Time resolution here is 1 second i want to convert it to 10ms这里的时间分辨率是 1 秒我想把它转换成 10ms 这里的时间分辨率是 1 秒我想把它转换成 10ms

i want to change the Time resolution in that table from 1s to be 10ms by subtraction the difference in time between each row multiply it by 100 and replicate each row with that number.我想将该表中的时间分辨率从 1 秒更改为 10 毫秒,方法是减去每行之间的时间差乘以 100,然后用该数字复制每一行。

for example: Row[n] will be repeated Time((n+1)-n)*100例如:Row[n] 将被重复 Time((n+1)-n)*100

when Time=2 sec (third row ) we have certain values combination that will stay the same till the next Row which time =22 sec (Fourth row) so the difference in time here is = 20 sec based on this i want (third row) to be repeated (20*100)当时间 = 2 秒(第三行)时,我们有某些值组合将保持不变,直到下一行,时间 = 22 秒(第四行),所以这里的时间差 = 20 秒,基于我想要的(第三行) ) 重复 (20*100)

Row[2] will be repeated (22-2)*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)

Create another column to hold the difference in the values of columns, for repetition reference and then do the operation like this:创建另一列来保存列值的差异,以供重复引用,然后执行如下操作:

import pandas as pd

# Sample dataframe
df = 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 rows
finalDf = df.loc[df.index.repeat(df.times)].reset_index(drop=True)
print(finalDf.head())

The output of print statement looks like: print语句的 output 如下所示:

  id  col1  col2  times
0  a     4     3      1
1  b     5     2      3
2  b     5     2      3
3  b     5     2      3
4  c     6     4      2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据唯一列值重复数据帧行n次,并为每一行重复创建一个具有不同值的新列 - Repeat dataframe rows n times according to the unique column values and to each row repeat create a new column with different values Pandas - 每次添加列时如何重复数据帧n次 - Pandas - How to repeat dataframe n times each time adding a column 如何将每个数据帧行重复 n 次(每行的 n 不同)? - How to repeat each dataframe row n number of times (n is different for each row)? 将DataFrame的每一行重复N次,创建一个新的DataFrame(N由另一列定义) - Repeat each row of a DataFrame N times to create a new DataFrame (N is defined by another column) Dataframe groupby某列并重复该行n次 - Dataframe groupby certain column and repeat the row n times 根据其他两列的值将 pandas 中的一行重复 n 次 - repeat a row in pandas n times based on the value of other two columns Pandas Dataframe 计算每组的时间差和两个不同组之间的时间差 - Pandas Dataframe calculate Time difference for each group and Time difference between two different groups 在矩阵中重复每行n次,并在每个部分旁边添加1 * n列 - Repeat each row n times in a matrix and add a column of 1*n next to each part 将同一行从 pandas dataframe 多次添加到新行,每次更改特定列中的值 - Add the same row multiple times from a pandas dataframe to a new one, each time altering a value in a specific column pandas - 根据列值复制每行'n'次 - pandas - Copy each row 'n' times depending on column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM