Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (17.6k points)

I have a dataframe..

s1 = pd.Series([5, 6, 7])

s2 = pd.Series([7, 8, 9])

df = pd.DataFrame([list(s1), list(s2)],  columns =  ["A", "B", "C"])

   A  B  C

0  5  6  7

1  7  8  9

[2 rows x 3 columns]

and I need to add a first row [2, 3, 4] to get..

   A  B  C

0  2  3  4

1  5  6  7

2  7  8  9

I've tried append() and concat() functions but somehow I can't find the right way how to do that.

Any ideas? Is there any direct way how to add/insert series to dataframe?

1 Answer

0 votes
by (41.4k points)

Using loc, assign row to a particular index:

 # adding a row

 df.loc[-1] = [2, 3, 4] 

 # shifting index

 df.index = df.index + 1 

# sorting by index

 df = df.sort_index()  

Browse Categories

...