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
  update values in one dataframe based on another dataframe - Pandas iliasb 2 1,447 Aug-14-2021, 12:38 PM
Last Post: jefsummers
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 1,083 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 879 Dec-17-2020, 01:08 PM
Last Post: palladium
  How to search for specific string in Pandas dataframe Coding_Jam 1 1,122 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  PANDAS: DataFrame | White Spaces & Special Character Removal traibr 1 1,894 Sep-10-2020, 07:02 PM
Last Post: eddywinch82
  No Output In Pandas DataFrame Query eddywinch82 1 910 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  strange error from pandas dataframe djf123 1 2,224 Jul-27-2020, 05:25 AM
Last Post: scidam
  Pandas DataFrame not updating HelpMePlease 3 1,195 Jul-11-2020, 07:19 PM
Last Post: jefsummers
  Pandas DataFrame visual Truman 8 1,706 Jul-10-2020, 06:11 AM
Last Post: hussainmujtaba
  Pandas DataFrame and unmatched column sritsv19 0 1,549 Jul-07-2020, 12:52 PM
Last Post: sritsv19

Forum Jump:

User Panel Messages

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