将 Pandas DataFrame 行复制到多个其他行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36875648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:08:02  来源:igfitidea点击:

Copy pandas DataFrame row to multiple other rows

pythonpandas

提问by Pedro Braz

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

修改 Pandas 数据框中的行子集

Changing certain values in multiple columns of a pandas DataFrame at once

一次更改 Pandas DataFrame 的多列中的某些值

Fastest way to copy columns from one DataFrame to another using pandas?

使用 Pandas 将列从一个 DataFrame 复制到另一个 DataFrame 的最快方法?

Selecting with complex criteria from pandas.DataFrame

从 pandas.DataFrame 中选择复杂的标准

The key difference between those and mine is that I need not to insert a single value, but a row.

它们和我的主要区别在于我不需要插入单个值,而是一行。

My problem is, I pick up a row of a dataframe, say df1. Thus I have a series.

我的问题是,我拿起了一行数据框,比如df1. 因此我有一个系列。

Now I have this other dataframe, df2, that I have selected multiple rows according to a criteria, and I want to replicate that series to all those row.

现在我有了另一个数据框,df2我根据一个条件选择了多行,我想将该系列复制到所有这些行。

df1:

df1:

Index/Col   A   B  C
1           0   0  0
2           0   0  0
3           1   2  3
4           0   0  0

df2:

df2:

Index/Col   A   B  C
1           0   0  0
2           0   0  0
3           0   0  0
4           0   0  0

What I want to accomplish is inserting df1[3] into the lines df2[2] and df3[3] for example. So something like the non working code:

例如,我想要完成的是将 df1[3] 插入到 df2[2] 和 df3[3] 行中。所以像非工作代码:

series = df1[3]
df2[df2.index>=2 and df2.index<=3] = series

returning

回来

df2:

df2:

Index/Col   A   B  C
1           0   0  0
2           1   2  3
3           1   2  3
4           0   0  0

回答by EdChum

Use locand 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 .valuesso that it's a numpy array. Otherwise you will get a ValueErroras there will be a shape mismatch as you're intending to overwrite 2 rows with a single row and if it's a Seriesthen it won't align as you desire:

使用loc并传递感兴趣的索引标签列表,在以下逗号之后:表示我们要设置所有列值,然后我们分配系列但调用属性.values,使其成为一个 numpy 数组。否则你会得到 aValueError因为会有形状不匹配,因为你打算用一行覆盖 2 行,如果它是 aSeries那么它不会像你想要的那样对齐:

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

回答by Ayush Srivastava

Suppose you have to copy certain rows and columns from dataframe to some another data frame do this. code

假设您必须将某些行和列从数据框中复制到另一个数据框中,请执行此操作。 code

    df2 = df.loc[x:y,a:b]   // x and y are rows bound and a and b are column 
                                bounds that you have to select