Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending rows to empty DataFrame not working

I have a piece of Python code that essentially reduces to the following:

import pandas as pd

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
l = [1607.8, 1688.17, 1605.66, 1469.03, 1312.65, 1451.74, 1445.26, 1350.94, 1371.27, 1201.6, 1240.59, 1291.43]

# First, construct a empty DataFrame.
df = pd.DataFrame(columns = months, dtype=float)

# Then, keep adding rows to DataFrame given by list l.
df.append([l])

print df

Expected output is:

      Jan      Feb      Mar      Apr      May      Jun      Jul      Aug  \
0  1607.8  1688.17  1605.66  1469.03  1312.65  1451.74  1445.26  1350.94   

       Sep     Oct      Nov      Dec  
0  1371.27  1201.6  1240.59  1291.43  

However what I get is the following:

Empty DataFrame
Columns: [Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
Index: []

If I replace df.append([l]) with the following, things work fine for me.

df = pd.DataFrame([l], columns = months)
df.columns = months

What am I doing wrong?

like image 589
Subhajit Kundu Avatar asked May 19 '18 22:05

Subhajit Kundu


People also ask

How do I append rows to an empty data frame?

Append Rows to Empty DataFramepandas. DataFrame. append() function is used to add the rows of other DataFrame to the end of the given DataFrame and return a new DataFrame object. Yields below output.

How do I append a row to a DataFrame?

You can create a DataFrame and append a new row to this DataFrame from dict, first create a Python Dictionary and use append() function, this method is required to pass ignore_index=True in order to append dict as a row to DataFrame, not using this will get you an error.

How do you initialize an empty data frame?

Example 1: Create an Empty DataFrame To create an empty DataFrame, pass no arguments to pandas. DataFrame() class. In this example, we create an empty DataFrame and print it to the console output. As we have provided no arguments, the columns array is empty and index array is empty.

How do you declare an empty DataFrame in Python?

You can create an empty dataframe by importing pandas from the python library. Later, using the pd. DataFrame(), create an empty dataframe without rows and columns as shown in the below example.


2 Answers

You should use pd.DataFrame.loc to add a row given a list l:

df.loc[len(df.index)+1] = l

The method pd.DataFrame.append is used to append one dataframe, or other appendable object, to an existing dataframe:

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None)

Append rows of other to the end of this frame


However, it is more efficient to form a list of lists and then call pd.DataFrame.append just once.

list_of_lists = []

for item in some_iterable:
    l = foo(item)
    list_of_lists.append(l)

df = df.append(pd.DataFrame(list_of_lists, columns=df.columns))

The reason for this is list.append is cheaper than pd.DataFrame.loc assignment.

like image 168
jpp Avatar answered Oct 18 '22 15:10

jpp


pd.append() does not happen in place. You need to save the output in order to use it.

like image 38
Ama Aje My Fren Avatar answered Oct 18 '22 14:10

Ama Aje My Fren