0

I am new to python and developing a code

I want to search for a word in a column and if a match is found, i want to insert an empty row below that.

My code is below

If df.columnname=='total':
  Df.insert

Could someone pls help me.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • 2
    Please provide some sample data and expected output .Thanks – anky Feb 01 '19 at 05:46
  • Use `loc` or `concat`: https://stackoverflow.com/questions/24284342/insert-a-row-to-pandas-dataframe/24287210 – LoMaPh Feb 01 '19 at 05:52
  • 3
    Please indent your code properly and provide [Minimum, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) – Andreas Feb 01 '19 at 06:02

2 Answers2

1

Do give the following a try:

>>>df

         id     Label
0         1       A
1         2       B
2         3       B
3         4       B
4         5       A
5         6       B
6         7       A
7         8       A
8         9       C
9        10       C
10       11       C

# Create a separate dataframe with the id of the rows to be duplicated
df1 = df.loc[df['Label']=='B', 'id']
# Join it back and reset the index
df = pd.concat(df,df1).sort_index()

>>>df
             id     Label
    0         1       A
    1         2       B
    2         2       NaN
    3         3       B
    4         3       NaN
    5         4       B
    6         4       NaN
    7         5       A
    8         6       B
    9         6       NaN
   10         7       A
   11         8       A
   12         9       C
   13        10       C
   14        11       C
kerwei
  • 1,822
  • 1
  • 13
  • 22
  • yes and you may want to set the entire row as nan so check the duplicates on other columns except labels and set to nan. :) +1 – anky Feb 01 '19 at 07:05
  • @Kerwei Your code do not added any empty row at particular position, It just concat two DF and sort based on index.Read Question carefully what exact he want. He wanted to insert empty row at specific position in DF. – Anonymous Feb 01 '19 at 07:25
  • I believe you can achieve the same effects as your output by adding the following 2 optional lines 1. ```df['id'] = df.index``` 2. ```df.set_index('id')``` – kerwei Feb 01 '19 at 07:38
0

Use below code:

from numpy import nan as Nan
import pandas as pd

df1 = pd.DataFrame({'Column1': ['A0', 'total', 'total', 'A3'],'Column2': ['B0', 'B1', 
'B2', 'B3'],'Column3': ['C0', 'C1', 'C2', 'C3'],'Column4': ['D0', 'D1', 'D2', 
'D3']},index=[0, 1, 2, 3])

count = 0
for index, row in df1.iterrows():
    if row["Column1"] == 'total':
        df1 = pd.DataFrame(np.insert(df1.values, index+1+count, values=[" "] 
             * len(df1.columns), axis=0),columns = df1.columns)
        count += 1
print (df1)

Input:

  Column1 Column2 Column3 Column4
0      A0      B0      C0      D0
1   total      B1      C1      D1
2   total      B2      C2      D2
3      A3      B3      C3      D3

Output:

  Column1 Column2 Column3 Column4
0      A0      B0      C0      D0
1   total      B1      C1      D1
2                                
3   total      B2      C2      D2
4                                
5      A3      B3      C3      D3
Anonymous
  • 659
  • 6
  • 16