Python pandas empty dataframe

pandas.DataFrame.empty#

True if Series/DataFrame is entirely empty (no items), meaning any of the axes are of length 0.

If Series/DataFrame is empty, return True, if not return False.

Return series without null values.

Return DataFrame with labels on given axis omitted where (all or any) data are missing.

If Series/DataFrame contains only NaNs, it is still not considered empty. See the example below.

An example of an actual empty DataFrame. Notice the index is empty:

>>> df_empty = pd.DataFrame('A' : []>) >>> df_empty Empty DataFrame Columns: [A] Index: [] >>> df_empty.empty True 

If we only have NaNs in our DataFrame, it is not considered empty! We will need to drop the NaNs to make the DataFrame empty:

>>> df = pd.DataFrame('A' : [np.nan]>) >>> df A 0 NaN >>> df.empty False >>> df.dropna().empty True 
>>> ser_empty = pd.Series('A' : []>) >>> ser_empty A [] dtype: object >>> ser_empty.empty False >>> ser_empty = pd.Series() >>> ser_empty.empty True 

Источник

Create an Empty Pandas Dataframe and Append Data

Empty Pandas Dataframe Cover Image

In this post, you’ll learn how to create an empty pandas dataframe and how to add data to them. Specifically, you’ll learn how to create the dataframe, create one with columns, add rows one-by-one and add rows via a loop.

Create an Empty Pandas Dataframe

To start things off, let’s begin by import the Pandas library as pd :

Creating a completely empty Pandas Dataframe is very easy. We simply create a dataframe object without actually passing in any data:

This returns the following:

Empty DataFrame Columns: [] Index: []

We can see from the output that the dataframe is empty.

However, we can also check if it’s empty by using the Pandas .empty attribute, which returns a boolean value indicating if the dataframe is empty:

Create an Empty Pandas Dataframe with Columns

There may be time when you know the columns you’ll want in a dataframe, but just don’t have the data for it yet (more on that in appending data to an empty dataframe below).

In order to do this, we can use the columns= parameter when creating the dataframe object to pass in a list of columns. Let’s create a dataframe with the following columns: Name, Age, Birth City, and Gender.

df = pd.DataFrame(columns=['Name', 'Age', 'Birth City', 'Gender']) print(df)

This prints out the following, indicating that we now have an empty dataframe but with columns attached to it:

Empty DataFrame Columns: [Name, Age, Birth City, Gender] Index: []

Create an Empty Pandas Dataframe with Columns and Indices

Similar to the situation above, there may be times when you know both column names and the different indices of a dataframe, but not the data.

We can accomplish creating such a dataframe by including both the columns= and index= parameters. Let’s create the same dataframe as above, but use the Name column as the index and fill in some sample indices:

df = pd.DataFrame( columns=['Age', 'Birth City', 'Gender'], index=['Jane', 'Melissa', 'John', 'Matt']) print(df)

This returns the following:

 Age Birth City Gender Jane NaN NaN NaN Melissa NaN NaN NaN John NaN NaN NaN Matt NaN NaN NaN

Now, technically, this isn’t an empty dataframe anymore. It’s simply a dataframe without data. We can verify this by using the .empty attribute:

Add Data to an Empty Dataframe

Now that we have our dataframe with both columns and indices, we can use .loc to add data to it. If you want to learn more about .loc , check out my tutorial here.

Let’s add some data to the record with index Jane:

df.loc['Jane',:] = [23, 'London', 'F'] print(df)

This now returns the following dataframe:

 Age Birth City Gender Jane 23 London F Melissa NaN NaN NaN John NaN NaN NaN Matt NaN NaN NaN

Append Data to an Empty Pandas Dataframe

Similar to adding rows one-by-one using the Pandas .loc , we can also use the .append() method to add rows.

The .append() method works by, well, appending a dataframe to another dataframe.

Let’s add the same row above using the append method:

df2 = pd.DataFrame( [['Jane', 23, 'London', 'F']], columns=['Name', 'Age', 'Birth City', 'Gender'] ) df = df.append(df2) print(df)

This returns the following dataframe:

 Name Age Birth City Gender 0 Jane 23 London F

To speed things up, we can also use a for loop to add data, as explore below.

Append to Empty Pandas Dataframe with a Loop

There may be times when you need to add multiple pieces of data to a dataframe. This can be simplified using a for loop, to, say, read multiple files and append them. To learn more about Python’s for loops, check out my post here.

In the example below, we’ll just work with different lists, but the method works the same if you read data from multiple iterative files.

We use the ignore_index = True argument to ensure that we create new indices. Otherwise, each index could be duplicated when reading in multiple dataframes.

df = pd.DataFrame( columns=['Name', 'Age', 'Birth City', 'Gender']) people = [ ['Jane', 23, 'London', 'F'], ['Melissa', 45, 'Paris', 'F'], ['John', 35, 'Toronto', 'M'] ] for person in people: temporary_df = pd.DataFrame([person], columns=['Name', 'Age', 'Birth City', 'Gender']) df = df.append(temporary_df, ignore_index=True) print(df)

This returns the following dataframe:

 Name Age Birth City Gender 0 Jane 23 London F 1 Melissa 45 Paris F 2 John 35 Toronto M

Conclusion

In this post, you learned how to create an empty dataframe, both with and without columns. Following that, you learned how to append data to an empty dataframe, both a single time as well as how to do it with a for loop. To learn more about the Pandas .DataFrame() class, check out the official documentation here. To learn more about the .append() method, check out the official documentation here.

Источник

Create Empty Dataframe in Pandas

Create Empty Dataframe in Pandas

DataFrame is a container class that stores and manipulates two-dimensional data organized in tabular format within the python data analysis library ‘Pandas‘. The data, rows, and columns are the three main components of a Pandas DataFrame. You can store different types of elements in each column of DataFrame. For instance, one column of a DataFrame can have all its elements as integers, while another column can have all its elements as string literals. However, you can make use of the attribute name ‘dtype‘ and return the object type of the column.
Oftentimes, to save memory, we may want to build an empty DataFrame. If you simply want to add records with two values rather than the entire DataFrame, for example, you can construct an empty DataFrame first and then append the values one by one. In this article, you will learn how to create an empty DataFrame in python using pandas. We’ll also talk about how to fill it with data later by adding rows or columns. So, let’s get started!

How to Create Empty DataFrame in Pandas?

Below are the three methods by which you can create an empty DataFrame in pandas:

1) Creating a dataframe without rows and columns

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. Note that the DataFrame() class available in the pandas library is similar to the constructor which is used to construct the class.

For example:

# import pandas library as pd import pandas as pd # create an Empty DataFrame object df = pd.DataFrame() print(df) # append columns to an empty DataFrame df['Name'] = ['Anna', 'Pete', 'Tommy'] df['Scores'] = [97, 600, 200] df['Questions'] = [2200, 75, 100] df
Name Scores Questions 0 Anna 97 2200 1 Pete 600 75 2 Tommy 200 100 

2) Create an empty dataframe with only columns

The second method is to create an empty dataframe with only columns in it. Later, to complete the DataFrame and add data into it, you can create and append the rows using the in-built append() method as shown in the below example.

For example:

# import pandas library as pd import pandas as pd # create an Empty DataFrame # object With column names only df = pd.DataFrame(columns = ['Name', 'Scores', 'Questions']) print(df) # append rows to an empty DataFrame df = df.append('Name' : 'Anna', 'Scores' : 97, 'Questions' : 2200>, ignore_index = True) df = df.append('Name' : 'Linda', 'Scores' : 30, 'Questions' : 50>, ignore_index = True) df = df.append('Name' : 'Tommy', 'Scores' : 17, 'Questions' : 220>, ignore_index = True)
df
Empty DataFrame Columns: [Name, Scoress, Questions] Index: [] Name Scores Questions 0 Anna 97 2200 1 Linda 30 50 2 Tommy 17 220 

3) Create an empty dataframe with column name and indices

Another method is to create the empty dataframe using columns and indices in it. As the indices are passed while creating the DataFrame, you can easily append the rows using the loc() function. It helps to retrieve data values from a dataset that are fitted in particular rows and columns based on index value passed. Check out the below example for a better understanding.

For example:

# import pandas library as pd import pandas as pd # create an Empty DataFrame object With # column names and indices df = pd.DataFrame(columns = ['Name', 'Scores', 'Questions'], index = ['a', 'b', 'c']) print("Empty DataFrame With NaN values : \n\n", df) # adding rows to an empty # dataframe at existing index df.loc['a'] = ['Anna', 50, 100] df.loc['b'] = ['Pete', 60, 120] df.loc['c'] = ['Tommy', 30, 60] df
Empty DataFrame With NaN values : Name Scores Questions a NaN NaN NaN b NaN NaN NaN c NaN NaN NaN Name Scores Questions a Anna 50 100 b Pete 60 120 c Tommy 30 60 

Conclusion

Data Analysis is one of the crucial steps for any programmer in the technical world. Also, many large companies make use of the huge data received from their customers and draw valuable insights from it. In this article, we studied how to create an empty DataFrame and store this large amount of data into it by appending rows and columns. All these methods are simple and highly recommended for making your coding efficient.

Источник

Читайте также:  Mysql python что это
Оцените статью