这是我的女友;
d = {'id':['abc','abc','abc','abc','def','def','def','ghj','ghj','ghj'],
'Section': ['1H','2H','3H','4H','1H','2H','3H','1H','2H','3H'],
'time':['00:00:00', '00:00:30', '00:01:00','00:01:30','00:00:00', '00:00:30', '00:01:00','00:00:00', '00:00:30', '00:01:00'],
'A': [0.1,0.2,0.5,0.1,0.1,0.2,0.6,0.3,0.1,0.1],
'B': [0.6,0.3,0.1,0.1,0.3,0.1,0.5,0.1,0.7,0.2]}
df = pd.DataFrame(d)
我想为每个ID创建一个30秒的新时间列。 我尝试了以下操作,但是,我不知道如何将输出转换为df中的列。
g = df.groupby('id')
tp = []
for idd, group in g:
tp.append(pd.timedelta_range('0 days 0 hours 0 minutes',periods=len(group), freq='30S'))
所需的输出
A B Section id tp
0 0.1 0.6 1H abc 00:00:00
1 0.2 0.3 2H abc 00:00:30
2 0.5 0.1 3H abc 00:01:00
3 0.1 0.1 4H abc 00:01:30
4 0.1 0.3 1H def 00:00:00
5 0.2 0.1 2H def 00:00:30
6 0.6 0.5 3H def 00:01:00
7 0.3 0.1 1H ghj 00:00:00
8 0.1 0.7 2H ghj 00:00:30
9 0.1 0.2 3H ghj 00:01:00
非常感谢您的帮助。
答案 0 :(得分:1)
将GroupBy.cumcount
用于计数器列,将to_timedelta
转换为第二时间增量,并将quiz
转换为第二时间增量:
<%= text_field(:quiz, :quiz_questions, :id) %>