goldfinger
goldfinger

Reputation: 1115

Python dataframe append row with default value

I am trying to append a row to a DataFrame in which the entire row should take on a default value. I see posts on adding a column with default values, but how do I append a row with default values?

Upvotes: 1

Views: 3346

Answers (2)

juuubbb
juuubbb

Reputation: 179

Create a single line dataframe and then use DataFrame.append():

df_to_append = pd.DataFrame([0, 0], columns=['column1', 'column2'])
your_df.append(df_to_append)

Checkout https://www.geeksforgeeks.org/python-pandas-dataframe-append/ For more information provide your piece of code

Upvotes: 0

ak_slick
ak_slick

Reputation: 1016

When you say append I think you mean add a new row to the bottom of a dataframe with a default value like this.

import pandas as pd
import numpy as np

df = pd.DataFrame({0: {0: 'March', 1: 2019, 2: 'A'},
 1: {0: 'April', 1: 2019, 2: 'E'},
 2: {0: 'May', 1: 2019, 2: 'F'}})

# where 1 is the default value
df.loc[3,:] = 1

Yeilds:

    0       1       2
0   March   April   May
1   2019    2019    2019
2   A       E       F
3   1       1       1

Upvotes: 2

Related Questions