add pandas series as a row to pandas dataframe

  • Last Update :
  • Techknowledgy :

Maybe an easier way would be to add the pandas.Series into the pandas.DataFrame with ignore_index=True argument to DataFrame.append(). Example -

DF = DataFrame()
for sample, data in D_sample_data.items():
   SR_row = pd.Series(data.D_key_value)
DF = DF.append(SR_row, ignore_index = True)

Demo -

In[1]: import pandas as pd

In[2]: df = pd.DataFrame([
   [1, 2],
   [3, 4]
], columns = ['A', 'B'])

In[3]: df
Out[3]:
   A B
0 1 2
1 3 4

In[5]: s = pd.Series([5, 6], index = ['A', 'B'])

In[6]: s
Out[6]:
   A 5
B 6
dtype: int64

In[36]: df.append(s, ignore_index = True)
Out[36]:
   A B
0 1 2
1 3 4
2 5 6

Another issue in your code is that DataFrame.append() is not in-place, it returns the appended dataframe, you would need to assign it back to your original dataframe for it to work. Example -

DF = DF.append(SR_row, ignore_index = True)

Something like this could work...

mydf.loc['newindex'] = myseries

Here is an example where I used it...

stats = df[['bp_prob', 'ICD9_prob', 'meds_prob', 'regex_prob']].describe()

stats
Out[32]:
   bp_prob ICD9_prob meds_prob regex_prob
count 171.000000 171.000000 171.000000 171.000000
mean 0.179946 0.059071 0.067020 0.126812
std 0.271546 0.142681 0.152560 0.207014
min 0.000000 0.000000 0.000000 0.000000
25 % 0.000000 0.000000 0.000000 0.000000
50 % 0.000000 0.000000 0.000000 0.013116
75 % 0.309019 0.065248 0.066667 0.192954
max 1.000000 1.000000 1.000000 1.000000

medians = df[['bp_prob', 'ICD9_prob', 'meds_prob', 'regex_prob']].median()

stats.loc['median'] = medians

stats
Out[36]:
   bp_prob ICD9_prob meds_prob regex_prob
count 171.000000 171.000000 171.000000 171.000000
mean 0.179946 0.059071 0.067020 0.126812
std 0.271546 0.142681 0.152560 0.207014
min 0.000000 0.000000 0.000000 0.000000
25 % 0.000000 0.000000 0.000000 0.000000
50 % 0.000000 0.000000 0.000000 0.013116
75 % 0.309019 0.065248 0.066667 0.192954
max 1.000000 1.000000 1.000000 1.000000
median 0.000000 0.000000 0.000000 0.013116

Convert the series to a dataframe and transpose it, then append normally.

srs = srs.to_frame().T
df = df.append(srs)

df.loc[len(df)] = ['Product 9', 99, 9.99, 8.88, 1.11]

df

append is deprecating so, the best choice would be to_frame().T

df1 = pd.DataFrame({
   'name': ['john', 'mark'],
   'job': ['manager', 'salesman'],
   'age': [43, 23]
})
ser1 = df1.iloc[-1]
pd.concat([df1, ser1.to_frame().T], ignore_index = True)

name job age
0 john manager 43
1 mark salesman 23
2 mark salesman 23

This would work as well:

df = pd.DataFrame()
new_line = pd.Series({
   'A2M': 4.059,
   'A2ML1': 4.28
}, name = 'HCC1419')
df = df.append(new_line, ignore_index = False)

Suggestion : 2

Iteratively appending rows to a DataFrame can be more computationally intensive than a single concatenate. A better solution is to append those rows to a list and then concatenate the list with the original DataFrame all at once.,Deprecated since version 1.4.0: Use concat() instead. For further details see Deprecated DataFrame.append and Series.append,If a list of dict/series is passed and the keys are all contained in the DataFrame’s index, the order of the columns in the resulting DataFrame will be unchanged.,A new DataFrame consisting of the rows of caller and the rows of other.

>>> df = pd.DataFrame([
      [1, 2],
      [3, 4]
   ], columns = list('AB'), index = ['x', 'y']) >>>
   df
A B
x 1 2
y 3 4
   >>>
   df2 = pd.DataFrame([
      [5, 6],
      [7, 8]
   ], columns = list('AB'), index = ['x', 'y']) >>>
   df.append(df2)
A B
x 1 2
y 3 4
x 5 6
y 7 8
>>> df.append(df2, ignore_index = True)
A B
0 1 2
1 3 4
2 5 6
3 7 8
>>> df = pd.DataFrame(columns = ['A']) >>>
   for i in range(5):
   ...df = df.append({
      'A': i
   }, ignore_index = True) >>>
   df
A
0 0
1 1
2 2
3 3
4 4
>>> pd.concat([pd.DataFrame([i], columns = ['A']) for i in range(5)],
   ...ignore_index = True)
A
0 0
1 1
2 2
3 3
4 4

Suggestion : 3

To append or add a row to DataFrame, create the new row as Series and use DataFrame.append() method.,In this example, we will create a DataFrame and append a new row to this DataFrame. The new row is initialized as a Python Dictionary and append() function is used to append the row to the dataframe.,In this tutorial, we shall learn how to append a row to an existing DataFrame, with the help of illustrative example programs.,append() is immutable. It does not change the DataFrame, but returns a new DataFrame with the row appended.

Following is the syntax of DataFrame.appen() function.

mydataframe = mydataframe.append(new_row, ignore_index = True)

Python Program

import pandas as pd

data = {
   'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
   'physics': [68, 74, 77, 78],
   'chemistry': [84, 56, 73, 69],
   'algebra': [78, 88, 82, 87]
}

#create dataframe
df_marks = pd.DataFrame(data)
print('Original DataFrame\n------------------')
print(df_marks)

new_row = {
   'name': 'Geo',
   'physics': 87,
   'chemistry': 92,
   'algebra': 97
}
#append row to the dataframe
df_marks = df_marks.append(new_row, ignore_index = True)

print('\n\nNew row added to DataFrame\n--------------------------')
print(df_marks)

Run the above Python program, and you shall see the original dataframe, and the dataframe appended with the new row.

Original DataFrame
-- -- -- -- -- -- -- -- --
name physics chemistry algebra
0 Somu 68 84 78
1 Kiku 74 56 88
2 Amol 77 73 82
3 Lini 78 69 87

New row added to DataFrame
-- -- -- -- -- -- -- -- -- -- -- -- --
name physics chemistry algebra
0 Somu 68 84 78
1 Kiku 74 56 88
2 Amol 77 73 82
3 Lini 78 69 87
4 Geo 87 92 97

Suggestion : 4

We can pass a list of series too in the dataframe.append() for appending multiple rows in dataframe. For example, we can create a list of series with same column names as dataframe i.e.,Adding a list as a row to the dataframe in pandas is very simple and easy. We can just pass the new index label in loc[] attribute and assign list object to it. For example,,While creating a series object we passed the index names same as index of dataframe. Contents of the dataframe returned are,,New DataFrame’s index is not same as original dataframe because ignore_index is passed as True in append() function. Also, for columns which were not present in the dictionary NaN value is added.

Pandas Dataframe provides a function dataframe.append() to add rows to a dataframe i.e.

DataFrame.append(other, ignore_index = False, verify_integrity = False, sort = None)
2._
    Name Age City Country
    a jack 34 Sydeny Australia
    b Riti 30 Delhi India
    c Vikas 31 Mumbai India
    d Neelu 32 Bangalore India
    e John 16 New York US
    f Mike 17 las vegas US

Let’s add a new row in above dataframe by passing dictionary i.e.

# Pass the row elements as key value pairs to append()
function
mod_df = df.append({
      'Name': 'Sahil',
      'Age': 22
   },
   ignore_index = True)

print('Modified Dataframe')
print(mod_df)

Complete example to add a dictionary as row to the dataframe is as follows,

import pandas as pd

# List of Tuples
students = [('jack', 34, 'Sydeny', 'Australia'),
   ('Riti', 30, 'Delhi', 'India'),
   ('Vikas', 31, 'Mumbai', 'India'),
   ('Neelu', 32, 'Bangalore', 'India'),
   ('John', 16, 'New York', 'US'),
   ('Mike', 17, 'las vegas', 'US')
]

#Create a DataFrame object
df = pd.DataFrame(students,
   columns = ['Name', 'Age', 'City', 'Country'],
   index = ['a', 'b', 'c', 'd', 'e', 'f'])

print('Original Dataframe')
print(df)

# Pass the row elements as key value pairs to append()
function
mod_df = df.append({
      'Name': 'Sahil',
      'Age': 22
   },
   ignore_index = True)

print('Modified Dataframe')
print(mod_df)

Output:

Original Dataframe
Name Age City Country
a jack 34 Sydeny Australia
b Riti 30 Delhi India
c Vikas 31 Mumbai India
d Neelu 32 Bangalore India
e John 16 New York US
f Mike 17 las vegas US
Modified Dataframe
Name Age City Country
0 jack 34 Sydeny Australia
1 Riti 30 Delhi India
2 Vikas 31 Mumbai India
3 Neelu 32 Bangalore India
4 John 16 New York US
5 Mike 17 las vegas US
6 Sahil 22 NaN NaN

Suggestion : 5

In this article, you have learned how to append a row to DataFrame using loc[], concat(), and append() methods. Using these you can append a row from list/dict at any position/index.,You can append a row to DataFrame by using append(), pandas.concat(), and loc[], in this article I will explain how to append a python list, dict (dictionary) as a row to pandas DataFrame, which ideally inserts a new row(s) to the DataFrame with elements specified by a list and dict.,If you have a list and want to append it to DataFrame use loc[]. For more similar examples, refer to how to append a list as a row to pandas DataFrame.,You can append one row or multiple rows to an existing pandas DataFrame in several ways, one way would be creating a list or dict with the details and appending it to DataFrame.

1._
# Below are quick example

# Append Row to DataFrame
list_row = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list_row

# Insert Dict to the dataframe using DataFrame.append()
new_row = {
   'Courses': 'Hyperion',
   'Fee': 24000,
   'Duration': '55days',
   'Discount': 1800
}
df2 = df.append(new_row, ignore_index = True)

# Append new row to specifig index name
df2 = df.append(pd.DataFrame([new_row], index = ['7'], columns = df.columns))

# Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index = df.columns, name = '7'))

# Using pandas.concat() to append a row
new_row = pd.DataFrame({
   'Courses': 'Hyperion',
   'Fee': 24000,
   'Duration': '55days',
   'Discount': 1800
}, index = [0])
df2 = pd.concat([new_row, df.loc[: ]]).reset_index(drop = True)

# Append specific row / index name using DataFrame.loc[]
df.loc['7',: ] = ['Hive', 25000, '45days', 2000]

# Append row in DataFrame using DataFrame.loc[]
df.loc['7'] = ['Hive', 25000, '45days', 2000]

Let’s create a pandas DataFrame from Dict with a few rows and columns and execute some examples to learn how to insert rows. Our DataFrame contains column names Courses, Fee, Duration, and Discount.

import pandas as pd
technologies = ({
   'Courses': ["Spark", "Hadoop", "pandas", "Java", "Pyspark"],
   'Fee': [20000, 25000, 30000, 22000, 26000],
   'Duration': ['30days', '40days', '35days', '60days', '50days'],
   'Discount': [1000, 2500, 1500, 1200, 3000]
})
df = pd.DataFrame(technologies)
print(df)

Yields below output.

Courses Fee Duration Discount
0 Spark 20000 30 days 1000
1 Hadoop 25000 40 days 2500
2 pandas 30000 35 days 1500
3 Java 22000 60 days 1200
4 Pyspark 26000 50 days 3000

If you have a list and want to append it to DataFrame use loc[]. For more similar examples, refer to how to append a list as a row to pandas DataFrame.

# New list to append Row to DataFrame
list = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list
print(df)
7._
# Append new row by specifig index name
df2 = df.append(pd.DataFrame([new_row], index = ['7'], columns = df.columns))
print(df2)

# Append row to the DataFrame using append()
df2 = df.append(pd.Series(new_row, index = df.columns, name = '7'))
print(df2)