Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Dataframe adding muliple empty row issue

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Before adding 221
After adding 225

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

Thanks

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading