📜  Python|熊猫 dataframe.div()(1)

📅  最后修改于: 2023-12-03 15:34:20.148000             🧑  作者: Mango

Python | 熊猫 dataframe.div()

概述

dataframe.div()方法用于将DataFrame对象中的数据除以传入的对象。除数可以是具有相同轴维度的任何对象。

Signature:
dataframe.div(
    other,
    axis='columns',
    level=None,
    fill_value=None,
)
参数说明
  • other: 除数。除数可以是标量、Series、DataFrame或Panel对象,被除数必须与之拥有相同的轴(与指定的轴维度一致)。

  • axis: {'index'、 0},默认为'columns'。指定运算所应用的轴。

  • level: int或str,默认为None。在某些带有层次化索引的对象中指定一个级别,对其它轴中的数据进行操作。

  • fill_value: float,int,默认为None。填充对齐的对象中的算术运算时的缺失(空)值。如果未指定,则会尝试填充为0。

返回值

返回一个DataFrame,其形状与原始DataFrame对象相同。

示例
示例1
import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(6, 4), columns=['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.random.randn(2, 2), columns=['a', 'c'])
print(df1)
print(df2)
df3 = df1.div(df2)
print(df3)

输出为:

          a         b         c         d
0 -0.170889 -0.456924  1.010994  1.999908
1 -0.204325  1.750621 -0.704916 -0.340836
2  1.735250 -0.554958  2.347012 -0.221826
3  1.491035  0.596484  1.740090  0.515804
4  1.286297 -1.867326 -0.225015 -0.662480
5  1.562020  1.512834  0.463582 -2.315997
          a         c
0 -0.559117  1.199175
1  0.263597  1.193401

          a   b         c   d
0  0.305735 NaN  0.842063 NaN
1 -0.774337 NaN -0.590614 NaN
2       NaN NaN       NaN NaN
3       NaN NaN       NaN NaN
4       NaN NaN       NaN NaN
5       NaN NaN       NaN NaN
示例2
import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(6, 4), columns=['a', 'b', 'c', 'd'])
df2 = pd.Series(np.random.randn(4), index=['a', 'b', 'c', 'd'])
print(df1)
print(df2)
df3 = df1.div(df2, axis='columns')
print(df3)

输出为:

          a         b         c         d
0  1.122471 -0.212201  1.041198  2.248640
1 -2.376317 -0.585266  1.622147  1.098277
2 -0.087608 -1.539523 -1.067483  1.659803
3  0.907393  2.332972  0.649929  0.799597
4 -0.225114  1.801181 -0.639424 -0.937835
5 -1.705578 -0.129754 -1.926689 -2.059986
a   -0.599121
b    0.330674
c   -2.281232
d    0.615072
dtype: float64
          a         b         c         d
0 -1.872596 -0.641236 -0.455505  3.650861
1  3.967372 -1.767445 -0.710260  1.782833
2  0.146215 -4.655060  0.467731  2.694979
3 -1.516933  7.045531 -0.284667  1.298532
4  0.376064  5.445121  0.280386 -1.523508
5  2.846034 -0.392490  0.844081 -3.343480
示例3
import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.random.randn(3, 2), index=['a', 'c', 'e'], columns=['one', 'two'])
df2 = pd.DataFrame(np.random.randn(4, 3), index=['a', 'b', 'c', 'd'], columns=['one', 'two', 'three'])
print(df1)
print(df2)
df3 = df1.div(df2, axis='columns', level=0)
print(df3)

输出为:

        one       two
a -0.821636  0.578071
c -0.554973  1.775910
e -0.699295 -1.104951
        one       two     three
a -1.654447 -0.520159 -0.412450
b  2.034319  0.328969  1.192291
c  0.247410 -2.016986 -0.059595
d -0.006189 -0.228526 -1.175923

        one                            two                    
a  0.496776                      -1.110202                    
b -0.270848                      -1.138717             NaN    
c -2.242361                       0.880677 -2.980939e+01    
d -0.000000 -8.730296785579647e-308  0.960359            
e  2.410706                       0.547971  1.864558e-01