Python Forum

Full Version: empty row in pandas dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a data frame that need to add empty line at the end of that. I also see from examples that it show NaN but I don't want that, I need it totally empty. I took and example code below that adds 3 empty lines with "NaN". What I want is only one empty row at the end of the list without "NaN".

import pandas as pd

df_new = pd.DataFrame([])

items = pd.DataFrame([
    ["Item Type","Item Name","Price AED"],
    ["laptops", "HP", 2400],
    ["laptops", "DELL", 3400],
    ["laptops", "ACER", 1400]
  ])

for i, row in items.iterrows():
  df_new = df_new.append(row)
  for _ in range(3):
      df_new = df_new.append(pd.Series(), ignore_index=True)

print(df_new)
Something like this.
import pandas as pd

items = pd.DataFrame([
    ["Item Type","Item Name","Price AED"],
    ["laptops", "HP", 2400],
    ["laptops", "DELL", 3400],
    ["laptops", "ACER", 1400]
  ])
>>> new_row = {0: '', 1: '', 2: ''}
>>> df = items.append(new_row, ignore_index=True)
>>> df
           0          1          2
0  Item Type  Item Name  Price AED
1    laptops         HP       2400
2    laptops       DELL       3400
3    laptops       ACER       1400
4                                 
(Jun-22-2021, 10:44 AM)snippsat Wrote: [ -> ]Something like this.
import pandas as pd

items = pd.DataFrame([
    ["Item Type","Item Name","Price AED"],
    ["laptops", "HP", 2400],
    ["laptops", "DELL", 3400],
    ["laptops", "ACER", 1400]
  ])
>>> new_row = {0: '', 1: '', 2: ''}
>>> df = items.append(new_row, ignore_index=True)
>>> df
           0          1          2
0  Item Type  Item Name  Price AED
1    laptops         HP       2400
2    laptops       DELL       3400
3    laptops       ACER       1400
4                                 

Thanks,

Is it possible not to show the row and column headings (0,1,2,3)?
>>> items
           0          1          2
0  Item Type  Item Name  Price AED
1    laptops         HP       2400
2    laptops       DELL       3400
3    laptops       ACER       1400
>>> 
>>> df = items.rename(columns=items.iloc[0]).drop(items.index[0])
>>> df
  Item Type Item Name Price AED
1   laptops        HP      2400
2   laptops      DELL      3400
3   laptops      ACER      1400
>>> df = df.append(pd.Series(), ignore_index=True)
>>> df
  Item Type Item Name Price AED
0   laptops        HP      2400
1   laptops      DELL      3400
2   laptops      ACER      1400
3       NaN       NaN       NaN
>>> 
>>> print(df.to_string(index=False))
Item Type Item Name Price AED
  laptops        HP      2400
  laptops      DELL      3400
  laptops      ACER      1400
      NaN       NaN       NaN