In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.