Python-将多行插入现有数据帧

Python-将多行插入现有数据帧,python,Python,我试图在现有数据框中插入两行,但似乎无法使其正常工作。现有的df是: df1 = pd.DataFrame({"a" : [1,2,3,4,5,6], "block" : [1, 1, 2, 2, 3, 3]}) 我想在第一个和第二个块行之后添加两个空行。我希望新的数据框如下所示: df_new = pd.DataFrame({"a" : [1,2,0,3,4,0,5,6], "block" : [1, 1, 0, 2, 2, 0, 3, 3]}) 行中不需要有任何值,我计划将它们用作其他内

我试图在现有数据框中插入两行,但似乎无法使其正常工作。现有的df是:

df1 = pd.DataFrame({"a" : [1,2,3,4,5,6], "block" : [1, 1, 2, 2, 3, 3]})
我想在第一个和第二个块行之后添加两个空行。我希望新的数据框如下所示:

df_new = pd.DataFrame({"a" : [1,2,0,3,4,0,5,6], "block" : [1, 1, 0, 2, 2, 0, 3, 3]})
行中不需要有任何值,我计划将它们用作其他内容的占位符。我已经考虑过添加行,但大多数帖子建议在数据帧的开头或结尾追加一行,这在我的情况下是行不通的

关于我的困境有什么建议吗

import pandas as pd

# Adds a new row to a DataFrame
# oldDf   - The DataFrame to which the row will be added
# index   - The index where the row will be added
# rowData - The new data to be added to the row
# returns - A new DataFrame with the row added
def AddRow(oldDf, index, rowData):
    newDf = oldDf.head(index)
    newDf = newDf.append(pd.DataFrame(rowData))
    newDf = newDf.append(oldDf.tail(-index))

    # Clean up the row indexes so there aren't any doubles.
    # Figured you may want this.
    newDf = newDf.reset_index(drop=True)

    return newDf

# Initial data
df1 = pd.DataFrame({"a" : [1,2,3,4,5,6], "block" : [1, 1, 2, 2, 3, 3]})

# Insert rows
blankRow = {"a": [0], "block": [0]}
df2 = AddRow(df1, 2, blankRow)
df2 = AddRow(df2, 5, blankRow)

为了提高性能,您可以删除AddRow()函数中对Reset_Index()的引用,并在添加所有行后调用它。

如果您总是希望在
列中的每组值之后插入新的零行,则可以执行以下操作:

从数据帧开始:

df1 = pd.DataFrame({"a" : [1,2,3,4,5,6], "block" : [1, 1, 2, 2, 3, 3]})
使用
列中的值将其分组:

gr = df1.groupby('block')
在每个组的末尾添加一行零:

df_new = gr.apply(lambda x: x.append({'a':0,'block':0}, ignore_index=True))
重置新数据帧的索引:

df_new.reset_index(drop = True, inplace=True)
您只需根据
列,然后根据每个组底部的占位符,将数据转换为新的数据帧即可

df1 = pd.DataFrame({"a" : [1,2,3,4,5,6], "block" : [1, 1, 2, 2, 3, 3]})

df1 # original data
Out[67]: 
   a  block
0  1      1
1  2      1
2  3      2
3  4      2
4  5      3
5  6      3

df_group = df1.groupby('block')

df = pd.DataFrame({"a" : [], "block" : []}) # final data to be appended

for name,group in df_group:
    group = pd.concat([group,pd.DataFrame({"a" : [0], "block" : [0]})])
    df = df.append(group, ignore_index=True)


df
Out[71]: 
   a  block
0  1      1
1  2      1
2  0      0
3  3      2
4  4      2
5  0      0
6  5      3
7  6      3
8  0      0

有没有一个特定的地点去的逻辑?我假设它是基于
列中的重复。是的,我希望这些插入行正好位于数据帧中每个块段的末尾之后。