Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append an empty row in dataframe using pandas

I am trying to append an empty row at the end of dataframe but unable to do so, even trying to understand how pandas work with append function and still not getting it.

Here's the code:

import pandas as pd  excel_names = ["ARMANI+EMPORIO+AR0143-book.xlsx"] excels = [pd.ExcelFile(name) for name in excel_names] frames = [x.parse(x.sheet_names[0], header=None,index_col=None).dropna(how='all') for x in excels] for f in frames:     f.append(0, float('NaN'))     f.append(2, float('NaN')) 

There are two columns and random number of row.

with "print f" in for loop i Get this:

                             0                 1 0                   Brand Name    Emporio Armani 2                 Model number            AR0143 4                  Part Number            AR0143 6                   Item Shape       Rectangular 8   Dial Window Material Type           Mineral 10               Display Type          Analogue 12                 Clasp Type            Buckle 14               Case Material   Stainless steel 16              Case Diameter    31 millimetres 18               Band Material           Leather 20                 Band Length  Women's Standard 22                 Band Colour             Black 24                 Dial Colour             Black 26            Special Features       second-hand 28                    Movement            Quartz 
like image 504
Mansoor Akram Avatar asked Oct 12 '16 12:10

Mansoor Akram


People also ask

Can you append to an empty pandas DataFrame?

Append Data to an Empty Pandas Dataframe loc , we can also use the . append() method to add rows. The . append() method works by, well, appending a dataframe to another dataframe.

How do you append a row in Python?

append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value. Parameters: other : DataFrame or Series/dict-like object, or list of these.

How do you add a row in panda?

You can use the df. loc() function to add a row to the end of a pandas DataFrame: #add row to end of DataFrame df. loc[len(df.

How do I add a row to a DataFrame list?

By using df. loc[index]=list you can append a list as a row to the DataFrame at a specified Index, In order to add at the end get the index of the last record using len(df) function. The below example adds the list ["Hyperion",27000,"60days",2000] to the end of the pandas DataFrame.


2 Answers

Add a new pandas.Series using pandas.DataFrame.append().

If you wish to specify the name (AKA the "index") of the new row, use:

df.append(pandas.Series(name='NameOfNewRow')) 

If you don't wish to name the new row, use:

df.append(pandas.Series(), ignore_index=True) 

where df is your pandas.DataFrame.

like image 102
srcerer Avatar answered Sep 18 '22 01:09

srcerer


You can add it by appending a Series to the dataframe as follows. I am assuming by blank you mean you want to add a row containing only "Nan". You can first create a Series object with Nan. Make sure you specify the columns while defining 'Series' object in the -Index parameter. The you can append it to the DF. Hope it helps!

from numpy import nan as Nan import pandas as pd  >>> df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'], ...                     'B': ['B0', 'B1', 'B2', 'B3'], ...                     'C': ['C0', 'C1', 'C2', 'C3'], ...                     'D': ['D0', 'D1', 'D2', 'D3']}, ...                     index=[0, 1, 2, 3])  >>> s2 = pd.Series([Nan,Nan,Nan,Nan], index=['A', 'B', 'C', 'D']) >>> result = df1.append(s2) >>> result      A    B    C    D 0   A0   B0   C0   D0 1   A1   B1   C1   D1 2   A2   B2   C2   D2 3   A3   B3   C3   D3 4  NaN  NaN  NaN  NaN 
like image 26
silent_dev Avatar answered Sep 19 '22 01:09

silent_dev