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
Question [Solved] Formatting cells of a pandas dataframe into an OpenDocument ods spreadsheet Calab 1 669 Mar-01-2025, 04:51 AM
Last Post: Calab
  Find duplicates in a pandas dataframe list column on other rows Calab 2 2,181 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,627 Aug-26-2024, 04:52 PM
Last Post: Calab
  Add NER output to pandas dataframe dg3000 0 1,153 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 2,690 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 2,017 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 2,082 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 3,426 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,920 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 2,660 Jan-10-2022, 07:19 PM
Last Post: moduki1

Forum Jump:

User Panel Messages

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