Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add rows to DataFrame in Pandas

Shulou Source: shulou.com Published: 2022-06-01 06:49:03 09月19日 Update

This article is about how to add lines to DataFrame in Pandas. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

You can use the df.loc () function to add a line to the end of Pandas DataFrame:

# add row to end of DataFramedf.locum [len (df.index)] = [value1, value2, value3,...]

You can use the df.append () function to append several lines of an existing DataFrame to the end of another DataFrame:

# append rows of df2 to end of existing DataFramedf = df.append (df2, ignore_index = True)

The following example shows how to use these functions in practice.

Example 1: add a row to Pandas DataFrame

The following code shows how to add a line to the end of the Pandas DataFrame:

Import pandas as pd # create DataFramedf = pd.DataFrame ({'points': [10, 12, 12, 14, 13, 18],' rebounds': [7, 7, 8, 13, 7, 4], 'assists': [11, 8, 10, 6, 6 5]}) # view DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 7 65 18 4 5 # add new row to end of DataFramedf.loclen (df.index)] = [20,7 5] # view updated DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 65 18 4 56 20 7 5 example 2: add a few lines to Pandas DataFrame

The following code shows how to add several lines of an existing DataFrame to the end of another DataFrame:

Import pandas as pd # create DataFramedf = pd.DataFrame ({'points': [10, 12, 12, 14, 13, 18],' rebounds': [7, 7, 8, 13, 7, 4], 'assists': [11, 8, 10, 6, 6 ) # view DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 7 65 18 4 5 # define second DataFramedf2 = pd.DataFrame ({'points': [21, 25, 26],' rebounds': [7, 7, 13] 'assists': [11,3,3]}) # add new row to end of DataFramedf = df.append (df2 Ignore_index = True) # view updated DataFramedf points rebounds assists0 10 7 111 12 7 82 12 8 103 14 13 64 13 7 65 18 4 56 21 7 117 25 38 26 13 3

Note that both DataFrame should have the same column name in order to successfully append the row of one DataFrame to the end of the other DataFrame.

Add: add an elegant column, be sure to be elegant!

Df ['new_colu'] =' 12 columns # add a column to DataFrame with the same value

DfOut [93]: one two three four new_colua 0 12 3 12b 4 5 6 7 12 c 8 9 10 11 12 d 12 13 14 15 12new_raw 3 3 3 12 12 Thank you for reading! This is the end of the article on "how to add rows to DataFrame in Pandas". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: At the end one line function elegance code content more examples articles plus one add good practical same success two examples articles see knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Shulou Tech Info macOS Linux