Reputation: 45
I'm trying to add a specific set rows that are in one data to another and I can't seem to figure it out. I'm sure there is a better way than adding one row at a time.
df1= pd.DataFrame([['a', 1, 'John'], ['b', 2, 'Marry'], ['c', 3, 'Debbie']], columns= ['letter', 'number', 'names'])
df2= pd.DataFrame([['w', 6, 'Jim'], ['x', 33, 'Paul'], ['r', 12, 'Logan']],['m', 10, 'Olive']],['j', 88, 'Maya']], columns= ['letter', 'number', 'names'])
I want to be able to take the first 3 rows from df2 that contain the information for Jim, Paul, and Logan and add it to df1.
Thank you in advance for all of the help.
Upvotes: 0
Views: 78
Reputation: 23217
You can use .append()
together with .iloc[]
as follows:
df1 = df1.append(df2.iloc[:3]).reset_index(drop=True)
Result:
letter number names
0 a 1 John
1 b 2 Marry
2 c 3 Debbie
3 w 6 Jim
4 x 33 Paul
5 r 12 Logan
.reset_index(drop=True)
is used to re-serialize the index
Upvotes: 1