我想得到偶数和奇数行之间的差异,然后添加奇数行ts值以及。
当前的实现方式确实正确地不同,但是遗漏了奇数行ts值。
import pandas as pd
data = [
['04-21 10:45:21.718'],
['04-21 10:45:22.718'],
['04-21 10:45:24.718'],
['04-21 10:45:28.718'],
['04-21 10:45:32.718'],
['04-21 10:45:38.718']
]
df = pd.DataFrame(data,columns=['ts'])
df['ts'] = pd.to_datetime(df['ts'],format="%m-%d %H:%M:%S.%f")
print(df)
df2 = pd.DataFrame(df['ts'].values[1::2] - df['ts'].values[::2],columns=['delta'])
df2['delta'] = df2['delta'].dt.total_seconds()
print(df2)
电流输出:
ts
0 1900-04-21 10:45:21.718
1 1900-04-21 10:45:22.718
2 1900-04-21 10:45:24.718
3 1900-04-21 10:45:28.718
4 1900-04-21 10:45:32.718
5 1900-04-21 10:45:38.718
delta
0 1.0
1 4.0
2 6.0
预期输出:
ts delta
1 1900-04-21 10:45:22.718 1.0
2 1900-04-21 10:45:28.718 4.0
3 1900-04-21 10:45:38.718 6.0
2条答案
按热度按时间oalqel3c1#
尝试:
输出:
o8x7eapl2#
下面是使用
groupby()
的方法输出: