python:在另一个数据框中的所有行中的所有行中的所有行中的行

发布于 2025-02-09 09:40:54 字数 576 浏览 1 评论 0原文

我有两个数据框,如下所示:

df1:
       A      B      C       D
index
    0  10000  20000  30000   40000

df2:
      time    type  A     B      C     D
index
    0 5/2020  unit  1000  4000   900   200
    1 6/2020  unit  7000  2000   600   4000

我想将df1.iloc [0]划分为DF2中的所有行以获取以下内容:

df:
      time    type  A     B    C       D
index
    0 5/2020  unit  10    5    33.33   200
    1 6/2020  unit  1.43  10   50      10

我尝试使用df1.iloc [0] .div(div( df2.iloc [:]),但这为我提供了除0索引以外的所有行。 任何建议将不胜感激。谢谢。

I have two DataFrames as follows:

df1:
       A      B      C       D
index
    0  10000  20000  30000   40000

df2:
      time    type  A     B      C     D
index
    0 5/2020  unit  1000  4000   900   200
    1 6/2020  unit  7000  2000   600   4000

I want to divide df1.iloc[0] by all rows in df2 to get the following:

df:
      time    type  A     B    C       D
index
    0 5/2020  unit  10    5    33.33   200
    1 6/2020  unit  1.43  10   50      10

I tried to use df1.iloc[0].div(df2.iloc[:]) but that gave me NaNs for all rows other than the 0 index.
Any suggestions would be greatly appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪也成诗 2025-02-16 09:40:54

让我们做

df2.update(df2.loc[:,df1.columns].rdiv(df1.iloc[0]))
df2
Out[861]: 
     time  type          A     B          C      D
0  5/2020  unit  10.000000   5.0  33.333333  200.0
1  6/2020  unit   1.428571  10.0  50.000000   10.0

Let us do

df2.update(df2.loc[:,df1.columns].rdiv(df1.iloc[0]))
df2
Out[861]: 
     time  type          A     B          C      D
0  5/2020  unit  10.000000   5.0  33.333333  200.0
1  6/2020  unit   1.428571  10.0  50.000000   10.0
甜柠檬 2025-02-16 09:40:54

另一种方法,使用numpy Divide

df2.update(np.divide(df.to_numpy()[:,:], df2.loc[:,df.columns]))
df2
    time    type    A           B       C           D
0   5/2020  unit    10.000000   5.0     33.333333   200.0
1   6/2020  unit    1.428571    10.0    50.000000   10.0

another way to do it, using numpy divide

df2.update(np.divide(df.to_numpy()[:,:], df2.loc[:,df.columns]))
df2
    time    type    A           B       C           D
0   5/2020  unit    10.000000   5.0     33.333333   200.0
1   6/2020  unit    1.428571    10.0    50.000000   10.0
一场春暖 2025-02-16 09:40:54

您可以使用 >。

df = df2.apply(lambda x:df1.iloc[0].div(x[df1.columns]), axis=1)

打印(DF):

               A     B          C      D
index                                   
0      10.000000   5.0  33.333333  200.0
1       1.428571  10.0  50.000000   10.0

You can use div.

df = df2.apply(lambda x:df1.iloc[0].div(x[df1.columns]), axis=1)

print(df):

               A     B          C      D
index                                   
0      10.000000   5.0  33.333333  200.0
1       1.428571  10.0  50.000000   10.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文