1

Time resolution here is 1 second i want to convert it to 10ms Time resolution here is 1 second i want to convert it to 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.

for example: Row[n] will be repeated 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)

Row[2] will be repeated (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)
ahmed e
  • 13
  • 3
  • Providing more information about the problem (i.e. the difference between what you expect to happen and what actually happens) will help folks help you. As it stands it's not clear what help you are asking for. Your approach seems line with other approaches to converting seconds to sub-second periods: https://stackoverflow.com/questions/6999726/how-can-i-convert-a-datetime-object-to-milliseconds-since-epoch-unix-time-in-p – Arthur Morris Sep 21 '20 at 02:14
  • 1
    Thank you Arthur i updated the description to be more clear, i appreciate your help – ahmed e Sep 21 '20 at 12:40

1 Answers1

1

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:

  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
Anup Tiwari
  • 474
  • 2
  • 5