Python Forum
empty row in pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
empty row in pandas dataframe
#1
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)
Reply
#2
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                                 
rwahdan likes this post
Reply
#3
(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)?
Reply
#4
>>> 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
rwahdan likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 962 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,091 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,269 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,243 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,758 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,527 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,100 Aug-14-2021, 12:38 PM
Last Post: jefsummers
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,916 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 2,320 Dec-17-2020, 01:08 PM
Last Post: palladium

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020