Copy Pandas Dataframe Row To Multiple Other Rows
Simple and practical question, yet I can't find a solution. The questions I took a look were the following: Modifying a subset of rows in a pandas dataframe Changing certain values
Solution 1:
Use loc
and pass a list of the index labels of interest, after the following comma the :
indicates we want to set all column values, we then assign the series but call attribute .values
so that it's a numpy array. Otherwise you will get a ValueError
as there will be a shape mismatch as you're intending to overwrite 2 rows with a single row and if it's a Series
then it won't align as you desire:
In [76]:
df2.loc[[2,3],:] = df1.loc[3].values
df2
Out[76]:
A B C
1 0 0 0
2 1 2 3
3 1 2 3
4 0 0 0
Solution 2:
Suppose you have to copy certain rows and columns from dataframe to some another data frame do this.
code
df2 = df.loc[x:y,a:b] // x and y arerows bound and a and b arecolumn
bounds that you have toselect
Post a Comment for "Copy Pandas Dataframe Row To Multiple Other Rows"