How to Add Row to Pandas DataFrame?
[Pages:3]How to Add Row to Pandas DataFrame?
Pandas DataFrame ? Add Row
You can add one or more rows to Pandas DataFrame using pandas.DataFrame.append() method.
The syntax of append() method is given below.
df = df.append(new_row, ignore_index=True) df = df.append(new_row, ignore_index=True)
where df is the DataFrame and new_row is the row appended to DataFrame.
append() returns a new DataFrame with the new row added to original dataframe. Original DataFrame is not modified by append() method.
Add Row (Python Dictionary) to Pandas DataFrame
In the following Python example, we will initialize a DataFrame and then add a Python Dictionary as row to the DataFrame, using append() method.
The python dictionary should contain the column names as keys and corresponding values as dictionary values.
Python Example
import pandas as pd #initialize a dataframe df = pd.DataFrame({ 'a':[14, 52, 46], 'b':[32, 85, 64], 'c':[88, 47, 36]}) #new row as dictionary row1 = {'a':11, 'b':22, 'c ':33} #append row to dataframe df = df.append(row1, ignore_index=True) #print the dataframe print(df)
import pandas as pd
#initialize a dataframe df = pd.DataFrame({
'a':[14, 52, 46], 'b':[32, 85, 64], 'c':[88, 47, 36]})
#new row as dictionary row1 = {'a':11, 'b':22, 'c':33}
#append row to dataframe df = df.append(row1, ignore_index=True)
#print the dataframe print(df)
Output
a b c 0 14 32 88 1 52 85 47 2 46 64 36 3 1a1 b2c2 33 0 14 32 88 1 52 85 47 2 46 64 36 3 11 22 33
append() method has created a new DataFrame and added the new row to this DataFrame.
Add Row (Pandas Series) to Pandas DataFrame
In the following Python example, we will initialize a DataFrame and then add a Pandas Series as row to the DataFrame, using append() method.
Python Example
import pandas as pd #initialize a dataframe df = pd.DataFrame({ 'a':[14, 52, 46], 'b':[32, 85, 64], 'c':[88, 47, 36]}) #new row as Pandas Series row1 = pd.Series(data={'a':11, 'b':22, 'c':33},
import pandas as pd
#initialize a dataframe df = pd.DataFrame({
'a':[14, 52, 46], 'b':[32, 85, 64], 'c':[88, 47, 36]})
#new row as Pandas Series row1 = pd.Series(data={'a':11, 'b':22, 'c':33}, name=len(df))
#append row to dataframe df = df.append(row1)
#print the dataframe print(df)
Output
a b c 0 14 32 88 1 52 85 47 2 46 64 36 3 1a1 b2c2 33 0 14 32 88 1 52 85 47 2 46 64 36 3 11 22 33
While initializing the Series, we assigned len(df) to name . name is translated to index in DataFrame. You can assign any value of your choice to the name based on requirement.
Summary
In this Pandas Tutorial, we learned how to add or append a row to Pandas DataFrame using append() method.
Python Pandas
Pandas Tutorial Pandas DataFrame - Change Column Names Pandas DataFrame - Add or Append Row Pandas DataFrame - Add new column Pandas DataFrame - Delete Column
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python pandas ii dataframes and other operations
- worksheet data handling using pandas
- how to add the new column in dataframe if we want to add the new
- basic plotting with python and matplotlib massachusetts institute of
- reportlab pdf generation user guide
- how to add row to pandas dataframe
- pandas dataframe notes university of idaho
- add element to dataframe
- reading and writing data files with python astrophysics
- cs 111 program design i university of illinois chicago