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
  PANDAS: DataFrame | Replace and others questions moduki1 2 209 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 97 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 2,092 Aug-14-2021, 12:38 PM
Last Post: jefsummers
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 1,232 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 1,010 Dec-17-2020, 01:08 PM
Last Post: palladium
  How to search for specific string in Pandas dataframe Coding_Jam 1 1,259 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  PANDAS: DataFrame | White Spaces & Special Character Removal traibr 1 2,146 Sep-10-2020, 07:02 PM
Last Post: eddywinch82
  No Output In Pandas DataFrame Query eddywinch82 1 1,006 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  strange error from pandas dataframe djf123 1 2,385 Jul-27-2020, 05:25 AM
Last Post: scidam
  Pandas DataFrame not updating HelpMePlease 3 1,322 Jul-11-2020, 07:19 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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