Python酷库之旅-第三方库Pandas(039)

CSDN 2024-07-27 14:35:01 阅读 64

目录

一、用法精讲

126、pandas.Series.rfloordiv方法

126-1、语法

126-2、参数

126-3、功能

126-4、返回值

126-5、说明

126-6、用法

126-6-1、数据准备

126-6-2、代码示例

126-6-3、结果输出

127、pandas.Series.rmod方法

127-1、语法

127-2、参数

127-3、功能

127-4、返回值

127-5、说明

127-6、用法

127-6-1、数据准备

127-6-2、代码示例

127-6-3、结果输出

128、pandas.Series.rpow方法

128-1、语法

128-2、参数

128-3、功能

128-4、返回值

128-5、说明

128-6、用法

128-6-1、数据准备

128-6-2、代码示例

128-6-3、结果输出

129、pandas.Series.combine方法

129-1、语法

129-2、参数

129-3、功能

129-4、返回值

129-5、说明

129-6、用法

129-6-1、数据准备

129-6-2、代码示例

129-6-3、结果输出

130、pandas.Series.combine_first方法

130-1、语法

130-2、参数

130-3、功能

130-4、返回值

130-5、说明

130-6、用法

130-6-1、数据准备

130-6-2、代码示例

130-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

126、pandas.Series.rfloordiv方法
126-1、语法

<code># 126、pandas.Series.rfloordiv方法

pandas.Series.rfloordiv(other, level=None, fill_value=None, axis=0)

Return Integer division of series and other, element-wise (binary operator rfloordiv).

Equivalent to other // series, but with support to substitute a fill_value for missing data in either one of the inputs.

Parameters:

other

Series or scalar value

level

int or name

Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value

None or float value, default None (NaN)

Fill existing missing (NaN) values, and any new element needed for successful Series alignment, with this value before computation. If data in both corresponding Series locations is missing the result of filling (at that location) will be missing.

axis

{0 or ‘index’}

Unused. Parameter needed for compatibility with DataFrame.

Returns:

Series

The result of the operation.

126-2、参数

126-2-1、other(必须)表示要与被除数进行操作的值,可以是一个标量,一个数组或另一个Series。

126-2-2、level(可选,默认值为None)一个整数或字符串,如果被除数是一个多层索引的Series,你可以通过这个参数指定你希望沿着哪个级别进行运算。

126-2-3、fill_value(可选,默认值为None)该值将在参与运算的元素中缺失时使用,如果Series中存在缺失值,可以用这个值填补,从而确保运算能够正常进行。

126-2-4、axis(可选,默认值为0)指定操作的轴,默认值为0(行)。

126-3、功能

        用于对Series中的元素进行逆向整数除法(即向下取整的除法)的函数,这种操作通常涉及到数据的除法运算,其中结果会向下取整。

126-4、返回值

        返回一个新的Series,其中包含了运算的结果。

126-5、说明

        无

126-6、用法
126-6-1、数据准备

126-6-2、代码示例

# 126、pandas.Series.rfloordiv方法

# 126-1、基本用法

import pandas as pd

s1 = pd.Series([2, 3, 4], index=['a', 'b', 'c'])

s2 = pd.Series([5, 6, 7], index=['a', 'b', 'c'])

result = s1.rfloordiv(s2)

print(result, end='\n\n')code>

# 126-2、使用level参数

import pandas as pd

arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]

index = pd.MultiIndex.from_arrays(arrays, names=('letters', 'numbers'))

s1 = pd.Series([10, 20, 30, 40], index=index)

s2 = pd.Series([2, 3, 4, 5], index=index)

result = s1.rfloordiv(s2, level='letters')code>

print(result, end='\n\n')code>

# 126-3、使用fill_value参数

import pandas as pd

s1 = pd.Series([1, 2], index=['a', 'b'])

s2 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])

result = s1.rfloordiv(s2, fill_value=1)

print(result, end='\n\n')code>

# 126-4、使用axis参数(主要适用于DataFrame)

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

df2 = pd.DataFrame({'A': [10, 20], 'B': [30, 40]})

result = df1.rfloordiv(df2, axis=0)

print(result)

126-6-3、结果输出

# 126、pandas.Series.rfloordiv方法

# 126-1、基本用法

# a 2

# b 2

# c 1

# dtype: int64

# 126-2、使用level参数

# letters numbers

# A 1 0

# 2 0

# B 1 0

# 2 0

# dtype: int64

# 126-3、使用fill_value参数

# a 10.0

# b 10.0

# c 30.0

# dtype: float64

# 126-4、使用axis参数(主要适用于DataFrame)

# A B

# 0 10 10

# 1 10 10

127、pandas.Series.rmod方法
127-1、语法

# 127、pandas.Series.rmod方法

pandas.Series.rmod(other, level=None, fill_value=None, axis=0)

Return Modulo of series and other, element-wise (binary operator rmod).

Equivalent to other % series, but with support to substitute a fill_value for missing data in either one of the inputs.

Parameters:

other

Series or scalar value

level

int or name

Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value

None or float value, default None (NaN)

Fill existing missing (NaN) values, and any new element needed for successful Series alignment, with this value before computation. If data in both corresponding Series locations is missing the result of filling (at that location) will be missing.

axis

{0 or ‘index’}

Unused. Parameter needed for compatibility with DataFrame.

Returns:

Series

The result of the operation.

127-2、参数

127-2-1、other(必须)表示要与Series中的元素进行模运算的值,可以是标量、数组或另一个 Series

127-2-2、level(可选,默认值为None)一个整数或字符串,适用于多层索引的Series,用于指定沿着哪个级别进行模运算。

127-2-3、fill_value(可选,默认值为None)在参与运算的元素中缺失时,使用此值填补,这样可以确保运算顺利进行。

127-2-4、axis(可选,默认值为0)一个整数,指定操作的轴,默认值为0(行)。

127-3、功能

        用于计算Series中元素的逆向取模运算(即右模运算)的函数。

127-4、返回值

        返回一个新的Series,其中包含模运算的结果。

127-5、说明

        无

127-6、用法
127-6-1、数据准备

127-6-2、代码示例

# 127、pandas.Series.rmod方法

# 127-1、基本用法

import pandas as pd

s1 = pd.Series([2, 3, 4], index=['a', 'b', 'c'])

s2 = pd.Series([5, 6, 7], index=['a', 'b', 'c'])

result = s1.rmod(s2)

print(result, end='\n\n')code>

# 127-2、使用level参数

import pandas as pd

arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]

index = pd.MultiIndex.from_arrays(arrays, names=('letters', 'numbers'))

s1 = pd.Series([10, 20, 30, 40], index=index)

s2 = pd.Series([2, 3, 4, 5], index=index)

result = s1.rmod(s2, level='letters')code>

print(result, end='\n\n')code>

# 127-3、使用fill_value参数

import pandas as pd

s1 = pd.Series([1, 2], index=['a', 'b'])

s2 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])

result = s1.rmod(s2, fill_value=1)

print(result, end='\n\n')code>

# 127-4、使用axis参数(主要适用于DataFrame)

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

df2 = pd.DataFrame({'A': [10, 20], 'B': [30, 40]})

result = df1.rmod(df2, axis=0)

print(result)

127-6-3、结果输出

# 127、pandas.Series.rmod方法

# 127-1、基本用法

# a 1

# b 0

# c 3

# dtype: int64

# 127-2、使用level参数

# letters numbers

# A 1 2

# 2 3

# B 1 4

# 2 5

# dtype: int64

# 127-3、使用fill_value参数

# a 0.0

# b 0.0

# c 0.0

# dtype: float64

# 127-4、使用axis参数(主要适用于DataFrame)

# A B

# 0 0 0

# 1 0 0

128、pandas.Series.rpow方法
128-1、语法

# 128、pandas.Series.rpow方法

pandas.Series.rpow(other, level=None, fill_value=None, axis=0)

Return Exponential power of series and other, element-wise (binary operator rpow).

Equivalent to other ** series, but with support to substitute a fill_value for missing data in either one of the inputs.

Parameters:

other

Series or scalar value

level

int or name

Broadcast across a level, matching Index values on the passed MultiIndex level.

fill_value

None or float value, default None (NaN)

Fill existing missing (NaN) values, and any new element needed for successful Series alignment, with this value before computation. If data in both corresponding Series locations is missing the result of filling (at that location) will be missing.

axis

{0 or ‘index’}

Unused. Parameter needed for compatibility with DataFrame.

Returns:

Series

The result of the operation.

128-2、参数

128-2-1、other(必须)表示要与Series中的元素进行幂运算的值,可以是标量、数组或另一个 Series

128-2-2、level(可选,默认值为None)一个整数或字符串,适用于具有多层索引的Series,用于指定沿哪个级别进行运算。

128-2-3、fill_value(可选,默认值为None)在参与运算的元素中缺失时,使用此值填补,这样可以确保运算顺利进行。

128-2-4、axis(可选,默认值为0)一个整数,指定操作的轴,默认值为0(行)。

128-3、功能

        用于计算Series中元素的逆向幂运算(即右幂运算)的方法。

128-4、返回值

        返回一个新的Series,其中包含逆向幂运算的结果。

128-5、说明

        无

128-6、用法
128-6-1、数据准备

128-6-2、代码示例

# 128、pandas.Series.rpow方法

# 128-1、基本用法

import pandas as pd

s1 = pd.Series([2, 3, 4], index=['a', 'b', 'c'])

s2 = pd.Series([5, 6, 7], index=['a', 'b', 'c'])

result = s1.rpow(s2)

print(result, end='\n\n')code>

# 128-2、使用level参数

import pandas as pd

arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]

index = pd.MultiIndex.from_arrays(arrays, names=('letters', 'numbers'))

s1 = pd.Series([10, 20, 30, 40], index=index)

s2 = pd.Series([2, 3, 4, 5], index=index)

result = s1.rpow(s2, level='letters')code>

print(result, end='\n\n')code>

# 128-3、使用fill_value参数

import pandas as pd

s1 = pd.Series([1, 2], index=['a', 'b'])

s2 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])

result = s1.rpow(s2, fill_value=1)

print(result, end='\n\n')code>

# 128-4、使用axis参数(主要适用于DataFrame)

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

df2 = pd.DataFrame({'A': [10, 20], 'B': [30, 40]})

result = df1.rpow(df2, axis=0)

print(result)

128-6-3、结果输出

# 128、pandas.Series.rpow方法

# 128-1、基本用法

# a 25

# b 216

# c 2401

# dtype: int64

# 128-2、使用level参数

# letters numbers

# A 1 1024

# 2 3486784401

# B 1 1152921504606846976

# 2 -4369436395329424031

# dtype: int64

# 128-3、使用fill_value参数

# a 10.0

# b 400.0

# c 30.0

# dtype: float64

# 128-4、使用axis参数(主要适用于DataFrame)

# A B

# 0 10 27000

# 1 400 2560000

129、pandas.Series.combine方法
129-1、语法

# 129、pandas.Series.combine方法

pandas.Series.combine(other, func, fill_value=None)

Combine the Series with a Series or scalar according to func.

Combine the Series and other using func to perform elementwise selection for combined Series. fill_value is assumed when value is missing at some index from one of the two objects being combined.

Parameters:

other

Series or scalar

The value(s) to be combined with the Series.

func

function

Function that takes two scalars as inputs and returns an element.

fill_value

scalar, optional

The value to assume when an index is missing from one Series or the other. The default specifies to use the appropriate NaN value for the underlying dtype of the Series.

Returns:

Series

The result of combining the Series with the other object.

129-2、参数

129-2-1、other(必须)表示另一个要与当前Series合并的Series对象。

129-2-2、func(必须)表示用于合并两个Series的函数,该函数需要接受两个参数,代表两个Series中的对应元素,并返回一个合并后的值。

129-2-3、value(可选,默认值为None)用于处理缺失值的填充值,如果other中有缺失值,则会使用该值代替。

129-3、功能

        用于合并两个Series对象的方法,它使用指定的函数(func)来结合两个Series的元素。

129-4、返回值

        返回一个新的Series,其中包含根据func合并后的结果。

129-5、说明

        无

129-6、用法
129-6-1、数据准备

129-6-2、代码示例

# 129、pandas.Series.combine方法

import pandas as pd

# 示例Series

s1 = pd.Series([5, 6, 3, None])

s2 = pd.Series([10, None, 8, 11])

# 自定义合并函数,选择较大的值

def combine_func(x, y):

return x if pd.notnull(x) and (pd.isnull(y) or x > y) else y

# 使用combine

result = s1.combine(s2, combine_func, fill_value=0)

print(result)

129-6-3、结果输出

# 129、pandas.Series.combine方法

# 0 10.0

# 1 6.0

# 2 8.0

# 3 11.0

# dtype: float64

130、pandas.Series.combine_first方法
130-1、语法

# 130、pandas.Series.combine_first方法

pandas.Series.combine_first(other)

Update null elements with value in the same location in ‘other’.

Combine two Series objects by filling null values in one Series with non-null values from the other Series. Result index will be the union of the two indexes.

Parameters:

other

Series

The value(s) to be used for filling null values.

Returns:

Series

The result of combining the provided Series with the other object.

130-2、参数

130-2-1、other(必须)表示另一个要与当前Series合并的Series对象。

130-3、功能

        用于合并两个Series对象的方法,它主要用于填充缺失值(NaN),该方法会将当前Series的值与另一个Series的值进行比较,如果当前Series中的某个元素是缺失值,那么就会用other中对应位置的值来填充。

130-4、返回值

        返回一个新的Series,其索引与当前Series相同,值为当前Series中的值和other中的值的结合。若当前值为缺失,则使用other中的对应值,否则保持当前值。

130-5、说明

        无

130-6、用法
130-6-1、数据准备

130-6-2、代码示例

# 130、pandas.Series.combine_first方法

import pandas as pd

# 示例Series

s1 = pd.Series([1, 2, None, 4])

s2 = pd.Series([None, 5, 6, None])

# 使用combine_first填充缺失值

result = s1.combine_first(s2)

print(result)

130-6-3、结果输出

# 130、pandas.Series.combine_first方法

# 0 1.0

# 1 2.0

# 2 6.0

# 3 4.0

# dtype: float64

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页


声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。