JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

How to Add a Row to a Pandas DataFrame

Author:JIYIK Last Updated:2025/05/02 Views:

Pandas is designed to load a fully populated DataFrame. We can pandas.DataFrameadd them one by one in . This can be done by using various methods, such as .loc, dictionary, pandas.concat()or DataFrame.append().


.loc [index]Add rows to a Pandas DataFrame with a list using

loc[index]The new list is treated as a new row and added to pandas.dataframeindex indexof .

Consider the following code:

# python 3.x
import pandas as pd

# List of Tuples
fruit_list = [("Orange", 34, "Yes")]
# Create a DataFrame object
df = pd.DataFrame(fruit_list, columns=["Name", "Price", "Stock"])
# Add new ROW
df.loc[1] = ["Mango", 4, "No"]
df.loc[2] = ["Apple", 14, "Yes"]
print(df)

result:

     Name  Price Stock
0  Orange     34   Yes
1   Mango      4    No
2   Apple     14   Yes

Add dictionary as row to Pandas DataFrame

append()You can add the dictionary keys and values ​​directly to a pandas dataframe as a row.

Consider the following code:

# python 3.x
import pandas as pd

# List of Tuples
fruit_list = [("Orange", 34, "Yes")]
# Create a DataFrame object
df = pd.DataFrame(fruit_list, columns=["Name", "Price", "Stock"])
# Add new ROW
df = df.append({"Name": "Apple", "Price": 23, "Stock": "No"}, ignore_index=True)
df = df.append({"Name": "Mango", "Price": 13, "Stock": "Yes"}, ignore_index=True)
print(df)

result:

     Name  Price Stock
0  Orange     34   Yes
1   Apple     23    No
2   Mango     13   Yes

Dataframe .appendmethod to add a row

.appendCan be used to DataFrameappend the rows of an additional to DataFramethe end of an original , and return a new DataFrame. DataFrameColumns from the new (not datafarmein the original ) are also added to the existing DataFrame, and the new cell values ​​are filled in NaN.

Consider the following code:

# python 3.x
import pandas as pd

# List of Tuples
fruit_list = [("Orange", 34, "Yes")]
# Create a DataFrame object
df = pd.DataFrame(fruit_list, columns=["Name", "Price", "Stock"])
print("Original DataFrame:")
print(df)
print(".............................")
print(".............................")
new_fruit_list = [("Apple", 34, "Yes", "small")]
dfNew = pd.DataFrame(new_fruit_list, columns=["Name", "Price", "Stock", "Type"])
print("Newly Created DataFrame:")
print(dfNew)
print(".............................")
print(".............................")
# append one dataframe to othher
df = df.append(dfNew, ignore_index=True)
print("Copying DataFrame to orignal...")
print(df)

ignore_index = TrueDataFrameThe new is ignored indexand DataFrameassigned a new index from the original .

Output:

Original DataFrame:
     Name  Price Stock
0  Orange     34   Yes
.............................
.............................
Newly Created DataFrame:
    Name  Price Stock   Type
0  Apple     34   Yes  small
.............................
.............................
Copying  DataFrame to original..:
     Name  Price Stock   Type
0  Orange     34   Yes    NaN
1   Apple     34   Yes  small

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to Convert DataFrame Column to String in Pandas

Publish Date:2025/05/02 Views:161 Category:Python

We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a

How to count the frequency of values in a Pandas DataFrame

Publish Date:2025/05/02 Views:84 Category:Python

Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().

How to get value from Pandas DataFrame cell

Publish Date:2025/05/02 Views:147 Category:Python

We'll look at using to get values ​​from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:198 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:192 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Subtracting Two Columns in Pandas DataFrame

Publish Date:2025/05/01 Views:120 Category:Python

Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi

Dropping columns by index in Pandas DataFrame

Publish Date:2025/05/01 Views:99 Category:Python

DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial