pandas 在第n行之后插入新行 [英] Pandas Insert a new row after every nth row

查看:84
本文介绍了 pandas 在第n行之后插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据框:

I have a dataframe that looks like below:

 **L_Type   L_ID    C_Type      E_Code**
    0       1           1         9
    0       1           2         9
    0       1           3         9
    0       1           4         9
    0       2           1         2
    0       2           2         2
    0       2           3         2
    0       2           4         2
    0       3           1         3
    0       3           2         3
    0       3           3         3
    0       3           4         3

我需要在每4行之后插入一个新行,并将第三列(C_Type)中的值增加01,如下表所示,同时保持与前两列相同的值,并且不希望最后一列中的任何值:

I need to insert a new row after every 4 row and increment the value in third column (C_Type) by 01 like below table while keeping the values same as first two columns and does not want any value in last column:

 L_Type     L_ID    C_Type          E_Code
    0       1           1           9
    0       1           2           9
    0       1           3           9
    0       1           4           9
    0       1           5           
    0       2           1           2
    0       2           2           2
    0       2           3           2
    0       2           4           2
    0       2           5           
    0       3           1           3
    0       3           2           3
    0       3           3           3
    0       3           4           3
    0       3           5           

我搜索了其他线程,但找不到确切的解决方案:

I have searched other threads but could not figure out the exact solution:

如何将n DataFrame插入到熊猫的第n行中?

在中插入新行熊猫数据框

推荐答案

您可以通过切片来查看行,将 1 添加到列 C_Type 0.5 进行索引,以进行100%的切片,因为 DataFrame.sort_index quicksort 。最后连接在一起,对索引进行排序,并通过 concat DataFrame.reset_index drop = True

You can seelct rows by slicing, add 1 to column C_Type and 0.5 to index, for 100% sorrect slicing, because default method of sorting in DataFrame.sort_index is quicksort. Last join together, sort index and create default by concat with DataFrame.reset_index and drop=True:

df['C_Type'] = df['C_Type'].astype(int)

df2 = (df.iloc[3::4]
         .assign(C_Type = lambda x: x['C_Type'] + 1, E_Code = np.nan)
         .rename(lambda x: x + .5))
df1 = pd.concat([df, df2], sort=False).sort_index().reset_index(drop=True)
print (df1)
    L_Type  L_ID  C_Type  E_Code
0        0     1       1     9.0
1        0     1       2     9.0
2        0     1       3     9.0
3        0     1       4     9.0
4        0     1       5     NaN
5        0     2       1     2.0
6        0     2       2     2.0
7        0     2       3     2.0
8        0     2       4     2.0
9        0     2       5     NaN
10       0     3       1     3.0
11       0     3       2     3.0
12       0     3       3     3.0
13       0     3       4     3.0
14       0     3       5     NaN

这篇关于 pandas 在第n行之后插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆