亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據時間列中兩個值之間的差異,將數據幀中的每一行重復 N 次

根據時間列中兩個值之間的差異,將數據幀中的每一行重復 N 次

汪汪一只貓 2023-08-22 15:52:21
這里的時間分辨率是 1 秒,我想將其轉換為 10 毫秒我想將該表中的時間分辨率從 1 秒更改為 10 毫秒,方法是減去每行之間的時間差,將其乘以 100,然后用該數字復制每行。例如: Row[n] 將被重復 Time((n+1)-n)*100當時間 = 2 秒(第三行)時,我們有某些值組合將保持不變,直到下一行,時間 = 22 秒(第四行),因此這里的時間差 = 20 秒,基于我想要的(第三行) ) 重復 (20*100)Row[2] 將重復 (22-2)*100import pandasimport pandas as pd# Dataframe from Excel sheetexcel_data_Outputs_df = pandas.read_excel(".xlsx", sheet_name='Outputs')excel_data_Inputs_df = pandas.read_excel("xlsx", sheet_name='Inputs')# Exclude All zeros columnsexcel_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)*100excel_data_Inputs_df['Time'] = excel_data_Inputs_df['Time'].fillna(0)excel_data_Inputs_df.Time=excel_data_Inputs_df.Time.astype(int)# Repeat Rowsnewexcel_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)
查看完整描述

1 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

創建另一列來保存列值的差異,以供重復參考,然后執行如下操作:


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())

語句的輸出print如下所示:


  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


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 1626 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號