Problem Statement
Let's say we have a DataFrame that contains multiple columns and rows. Ans we want to add one or multiple rows to this DataFrame then we can use one of the solutions explained in this post. We can create the DataFrame as below.
import pandas as pd
# create a dataframe
df = pd.DataFrame({
'product_name': ['p1', 'p2', 'p3', 'p4'],
'price': [100.0, 30.0, 200.0, 140.0],
'quantity': [14, 67, 90, 40]
});
╒════╤════════════════╤═════════╤════════════╕
│ │ product_name │ price │ quantity │
╞════╪════════════════╪═════════╪════════════╡
│ 0 │ p1 │ 100 │ 14 │
├────┼────────────────┼─────────┼────────────┤
│ 1 │ p2 │ 30 │ 67 │
├────┼────────────────┼─────────┼────────────┤
│ 2 │ p3 │ 200 │ 90 │
├────┼────────────────┼─────────┼────────────┤
│ 3 │ p4 │ 140 │ 40 │
╘════╧════════════════╧═════════╧════════════╛
The pandas.concat() function is used to add new rows to an existing DataFrame. This is useful when you have data in different formats that you want to combine into a single DataFrame. For example, you may have data in CSV format and data in JSON format that you want to combine into a single DataFrame.
Code example
import pandas as pd
# create a dataframe
df_1 = pd.DataFrame({
'product_name': ['p1', 'p2', 'p3', 'p4'],
'price': [100.0, 30.0, 200.0, 140.0],
'quantity': [14, 67, 90, 40]
});
# create another DataFrame
new_rows_data = {"product_name": ['p5'], 'price': [340.0], 'quantity': [70]}
# create new DataFrame from the above dictionary
df_2 = pd.DataFrame(new_rows_data)
df = pd.concat([df_1, df_2], ignore_index = True)
print(df)
Output
╒════╤════════════════╤═════════╤════════════╕
│ │ product_name │ price │ quantity │
╞════╪════════════════╪═════════╪════════════╡
│ 0 │ p1 │ 100 │ 14 │
├────┼────────────────┼─────────┼────────────┤
│ 1 │ p2 │ 30 │ 67 │
├────┼────────────────┼─────────┼────────────┤
│ 2 │ p3 │ 200 │ 90 │
├────┼────────────────┼─────────┼────────────┤
│ 3 │ p4 │ 140 │ 40 │
├────┼────────────────┼─────────┼────────────┤
│ 4 │ p5 │ 340 │ 70 │
╘════╧════════════════╧═════════╧════════════╛
In Python, a DataFrame is a two-dimensional data structure, similar to a table in a relational database. You can add data to a DataFrame in various ways, but one common method is to use the append() function.
The append() function takes a single argument, which is a dictionary. The keys in the dictionary become the columns in the DataFrame, and the values become the rows. For example, if you have a dictionary with the keys "a" and "b" and the values 1 and 2, the resulting DataFrame will have two columns (a and b) and one row (1, 2).
You can append multiple dictionaries to a DataFrame by passing a list of dictionaries to the append() function. The resulting DataFrame will have the same number of columns as the original DataFrame, but the number of rows will be equal to the sum of the number of rows in the original DataFrame and the number of dictionaries passed to the append() function.
In summary, the append() function is a convenient way to add data to a DataFrame. It can take a dictionary as an argument, and the keys in the dictionary become the columns in the resulting DataFrame.
Code example
import pandas as pd
# create a dataframe
df = pd.DataFrame({
'product_name': ['p1', 'p2', 'p3', 'p4'],
'price': [100.0, 30.0, 200.0, 140.0],
'quantity': [14, 67, 90, 40]
});
row_dict = {"product_name": 'p5', 'price': 340.0, 'quantity': 70}
# add a new row to df DataFrame
df = df.append(row_dict, ignore_index=True)
print(df)
Output
╒════╤════════════════╤═════════╤════════════╕
│ │ product_name │ price │ quantity │
╞════╪════════════════╪═════════╪════════════╡
│ 0 │ p1 │ 100 │ 14 │
├────┼────────────────┼─────────┼────────────┤
│ 1 │ p2 │ 30 │ 67 │
├────┼────────────────┼─────────┼────────────┤
│ 2 │ p3 │ 200 │ 90 │
├────┼────────────────┼─────────┼────────────┤
│ 3 │ p4 │ 140 │ 40 │
├────┼────────────────┼─────────┼────────────┤
│ 4 │ p5 │ 340 │ 70 │
╘════╧════════════════╧═════════╧════════════╛
You may see the below warning when executing the above code.
FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
Adding a new row to a pandas DataFrame can be done using the loc[] method. This method allows you to specify the location of the new row, as well as the data that will go in that row. In this example, we will add a new row at the end of the DataFrame.
Code example
import pandas as pd
# create a dataframe
df = pd.DataFrame({
'product_name': ['p1', 'p2', 'p3', 'p4'],
'price': [100.0, 30.0, 200.0, 140.0],
'quantity': [14, 67, 90, 40]
});
# add new row using df.loc[] method
df.loc[len(df.index)] = ['p5', 300.0, 35]
print(df)
Output
╒════╤════════════════╤═════════╤════════════╕
│ │ product_name │ price │ quantity │
╞════╪════════════════╪═════════╪════════════╡
│ 0 │ p1 │ 100 │ 14 │
├────┼────────────────┼─────────┼────────────┤
│ 1 │ p2 │ 30 │ 67 │
├────┼────────────────┼─────────┼────────────┤
│ 2 │ p3 │ 200 │ 90 │
├────┼────────────────┼─────────┼────────────┤
│ 3 │ p4 │ 140 │ 40 │
├────┼────────────────┼─────────┼────────────┤
│ 4 │ p5 │ 300 │ 35 │
╘════╧════════════════╧═════════╧════════════╛
Explanation of the above code example
0 Comments