如何在pandas中通过每次对列进行子访问来创建新行?

2024-03-28 12:30:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个像这样的新数据帧

这是我的数据

queue_id     agent_id login_duration   Hour DayofWeek   logout_reason_id
1   12  1593    2.280833    0   Tue 11.0
2   12  1593    2.280278    0   Tue 11.0
3   12  1593    2.279722    0   Tue 11.0
4   13  1593    2.279444    0   Tue 11.0
5   13  1593    2.279167    0   Tue 11.0

我想要这个:

queue_id     agent_id login_duration   Hour Day of Week logout_reason_id
1   12  1593    2.280833    0   Tue 11.0
2   12  1593    1.280833    1   Tue 11.0
3   12  1593    0.280833    2   Tue 11.0
4   12  1593    2.280278    0   Tue 11.0
5   12  1593    1.280278    1   Tue 11.0
6   12  1593    0.280278    2   Tue 11.0

Tags: of数据idqueueloginagentlogoutreason
1条回答
网友
1楼 · 发布于 2024-03-28 12:30:13

假设我对你想做的事情是正确的,我认为这是一个可行的解决方案

li = []

for i in df.index:
    temp = int(np.floor(df.loc[i,'login_duration']))
    temp_df = df[i:i+1]
    for j in range(1, temp+1):
        temp_df = temp_df.append(df[i:i+1], ignore_index=True)
        temp_df.loc[j, 'login_duration'] += -j
        temp_df.loc[j, 'Hour'] += j
    li.append(temp_df)

df = li[0]
for d in li[1:]:
    df = df.append(d, ignore_index=True)

我肯定这不是最好的解决办法,但我认为应该行得通。它适用于你提供的数据。你知道吗

相关问题 更多 >