0

How do I add a new row to my dataframe, with values that are based on the column names?

For example

Dog = 'happy'
Cat = 'sad'
df = pd.DataFrame(columns=['Dog', 'Cat'])

I want to add a new line to the dataframe where is pulls in the variable of the column heading

      Dog   Cat
0   happy   sad

2 Answers 2

1

You can try append:

df.append({'Dog':Dog,'Cat':Cat}, ignore_index=True)

Output:

     Dog  Cat
0  happy  sad
Sign up to request clarification or add additional context in comments.

1 Comment

append() is removed in pandas 2.0. The docs say: "Removed deprecated Series.append(), DataFrame.append(), use concat() instead (GH 35407)"
0

You can use append in a different way(other than mentioned above one)

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
df

output:
   A  B
0  1  2
1  3  4

df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
df.append(df2)

output:
   A  B
0  1  2
1  3  4
0  5  6
1  7  

1 Comment

append() is removed in pandas 2.0. The docs say: "Removed deprecated Series.append(), DataFrame.append(), use concat() instead (GH 35407)"

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.