1

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?

1

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.