Home > Software engineering >  Pandas Dataframe adding muliple empty row issue
Pandas Dataframe adding muliple empty row issue

Time:10-18

I have test the code to add multiple empty row to Pandas DataFrame it worked.

import pandas as pd

df = pd.DataFrame({'name': ["James", "White", "John"],
                   'rebounds': [7, 7, 8]})

for i in range(100):
    dft =  df.columns.values.tolist()
    s = pd.Series(dft)
    df.loc[len(df)] =s
print(len(df.index))

In my application I have this code, its adding 16 empty rows but it adds only 4 rows.

    print(f"Before adding {len(df3.index)}")
    dft =  df3.columns.values.tolist()
    s = pd.Series(dft)
    for i in range(16):
        df3.loc[len(df3)] =s

    print(f"After adding {len(df3.index)}")

Output

Before adding 221
After adding 225

why its not adding 16 empty rows in my application code? How to troubleshoot this issue?

Thanks

CodePudding user response:

You might have duplicated indices. For example if you array has 221 rows but an indice 222, no row will be added.

A more robust method could be to concat:

N = 4
df = pd.concat([df, pd.DataFrame(columns=df.columns,
                                 index=range(len(df), len(df) N))
                ])

output:

    name rebounds
0  James        7
1  White        7
2   John        8
3    NaN      NaN
4    NaN      NaN
5    NaN      NaN
6    NaN      NaN
  • Related