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

目录

一、用法精讲

pandas.DataFrame.to_csv%E5%87%BD%E6%95%B0-toc" style="margin-left:120px;">466、pandas.DataFrame.eq方法

466-1、语法

466-2、参数

466-3、功能

466-4、返回值

466-5、说明

466-6、用法

466-6-1、数据准备

466-6-2、代码示例

466-6-3、结果输出

pandas.DataFrame.combine%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">467、pandas.DataFrame.combine方法

467-1、语法

467-2、参数

467-3、功能

467-4、返回值

467-5、说明

467-6、用法

467-6-1、数据准备

467-6-2、代码示例

467-6-3、结果输出

pandas.DataFrame.combine_first%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">468、pandas.DataFrame.combine_first方法

468-1、语法

468-2、参数

468-3、功能

468-4、返回值

468-5、说明

468-6、用法

468-6-1、数据准备

468-6-2、代码示例

468-6-3、结果输出

pandas.DataFrame.apply%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">469、pandas.DataFrame.apply方法

469-1、语法

469-2、参数

469-3、功能

469-4、返回值

469-5、说明

469-6、用法

469-6-1、数据准备

469-6-2、代码示例

469-6-3、结果输出

pandas.DataFrame.applymap%E6%96%B9%E6%B3%95-toc" style="margin-left:120px;">470、pandas.DataFrame.applymap方法

470-1、语法

470-2、参数

470-3、功能

470-4、返回值

470-5、说明

470-6、用法

470-6-1、数据准备

470-6-2、代码示例

470-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

pandas.DataFrame.to_csv%E5%87%BD%E6%95%B0">466、pandas.DataFrame.eq方法
466-1、语法
python"># 466、pandas.DataFrame.eq方法
pandas.DataFrame.eq(other, axis='columns', level=None)
Get Equal to of dataframe and other, element-wise (binary operator eq).Among flexible wrappers (eq, ne, le, lt, ge, gt) to comparison operators.Equivalent to ==, !=, <=, <, >=, > with support to choose axis (rows or columns) and level for comparison.Parameters:
other
scalar, sequence, Series, or DataFrame
Any single or multiple element data structure, or list-like object.axis
{0 or ‘index’, 1 or ‘columns’}, default ‘columns’
Whether to compare by the index (0 or ‘index’) or columns (1 or ‘columns’).level
int or label
Broadcast across a level, matching Index values on the passed MultiIndex level.Returns:
DataFrame of bool
Result of the comparison.
466-2、参数

466-2-1、other(必须)标量、Series、DataFrame或array-like对象,与DataFrame进行比较的对象,如果other是标量,则DataFrame中的每个元素都会与该标量进行比较;如果是另一个DataFrame或Series,则逐元素进行比较。

466-2-2、axis(可选,默认值为'columns'){0, 1, 'index', 'columns'},确定运算的轴,如果设为0或'index',则对行标签进行对齐操作;如果设为1或'columns',则对列标签进行对齐操作,通常只有在other是DataFrame或Series时才需要指定axis参数。

466-2-3、level(可选,默认值为None)用于在多层索引(MultiIndex)中匹配特定级别,如果DataFrame或other有MultiIndex,level指定要在MultiIndex的哪个级别进行对齐。

466-3、功能

        对DataFrame中的每个元素与另一个对象(如标量、Series、DataFrame等)进行逐元素比较,判断其是否等于该对象,该方法常用于检查两个数据集之间的相似性或验证数据的一致性。

466-4、返回值

        返回一个布尔类型的DataFrame,其中每个元素表示原始DataFrame中对应元素是否等于other中的对应元素。即,如果df[i,j] == other[i,j],则结果DataFrame的该元素为True,否则为False。

466-5、说明

        无

466-6、用法
466-6-1、数据准备
python">无
466-6-2、代码示例
python"># 466、pandas.DataFrame.eq方法
import pandas as pd
# 创建示例DataFrame
df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]
})
# 比较DataFrame中的元素是否等于标量4
result = df.eq(4)
print(result)
466-6-3、结果输出
python"># 466、pandas.DataFrame.eq方法
#        A      B
# 0  False   True
# 1  False  False
# 2  False  False
pandas.DataFrame.combine%E6%96%B9%E6%B3%95">467、pandas.DataFrame.combine方法
467-1、语法
python"># 467、pandas.DataFrame.combine方法
pandas.DataFrame.combine(other, func, fill_value=None, overwrite=True)
Perform column-wise combine with another DataFrame.Combines a DataFrame with other DataFrame using func to element-wise combine columns. The row and column indexes of the resulting DataFrame will be the union of the two.Parameters:
other
DataFrame
The DataFrame to merge column-wise.func
function
Function that takes two series as inputs and return a Series or a scalar. Used to merge the two dataframes column by columns.fill_value
scalar value, default None
The value to fill NaNs with prior to passing any column to the merge func.overwrite
bool, default True
If True, columns in self that do not exist in other will be overwritten with NaNs.Returns:
DataFrame
Combination of the provided DataFrames.
467-2、参数

467-2-1、other(必须)要与当前DataFrame组合的另一个DataFrame,两个DataFrame的索引和列会相互对齐。

467-2-2、func(必须)用于组合两个DataFrame中元素的函数,该函数必须接受两个参数,并返回一个值,该函数将应用于两个DataFrame中的每对元素。

467-2-3、fill_value(可选,默认值为None)标量值,在操作中用于填充缺失值的填充值,如果其中一个DataFrame中某个位置存在NaN或缺失值,而另一个DataFrame中相应位置有值,则使用fill_value进行替代。

467-2-4、overwrite(可选,默认值为True)布尔值,如果为True,则当self和other中对应元素是缺失值时,后者的值会覆盖前者的值;如果为False,则保持第一个DataFrame中的缺失值,除非使用fill_value进行替代。

467-3、功能

        用于逐元素地将两个DataFrame组合在一起,通过指定一个函数func,定义如何组合数据。此方法可以用于两个DataFrame之间进行灵活的元素级操作,例如选择最大值、最小值或其他自定义的组合方式。

467-4、返回值

        返回一个新的DataFrame,其中每个元素由func函数在self和other的对应元素上进行计算得到,索引和列名与输入的两个DataFrame对齐。

467-5、说明

        无

467-6、用法
467-6-1、数据准备
python">无
467-6-2、代码示例
python"># 467、pandas.DataFrame.combine方法
import pandas as pd
import numpy as np
# 创建两个示例DataFrame
df1 = pd.DataFrame({'A': [1, 2, np.nan], 'B': [4, np.nan, 6]})
df2 = pd.DataFrame({'A': [3, 4, 5], 'B': [np.nan, 2, 1]})
# 定义一个组合函数,选择两个DataFrame中的最大值
def comb_func(x, y):return np.maximum(x, y)
# 使用combine进行组合
result = df1.combine(df2, comb_func, fill_value=0)
print(result)
467-6-3、结果输出
python"># 467、pandas.DataFrame.combine方法
#      A    B
# 0  3.0  4.0
# 1  4.0  2.0
# 2  5.0  6.0
pandas.DataFrame.combine_first%E6%96%B9%E6%B3%95">468、pandas.DataFrame.combine_first方法
468-1、语法
python"># 468、pandas.DataFrame.combine_first方法
pandas.DataFrame.combine_first(other)
Update null elements with value in the same location in other.Combine two DataFrame objects by filling null values in one DataFrame with non-null values from other DataFrame. The row and column indexes of the resulting DataFrame will be the union of the two. The resulting dataframe contains the ‘first’ dataframe values and overrides the second one values where both first.loc[index, col] and second.loc[index, col] are not missing values, upon calling first.combine_first(second).Parameters:
other
DataFrame
Provided DataFrame to use to fill null values.Returns:
DataFrame
The result of combining the provided DataFrame with the other object.
468-2、参数

468-2-1、other(必须)与当前DataFrame进行组合的另一个DataFrame,两个 DataFrame 按照索引和列进行对齐。

468-3、功能

        用于将两个DataFrame逐元素进行比较,并优先选择第一个DataFrame中的非空值,如果第一个DataFrame中的值是缺失值(NaN),则使用第二个DataFrame中的相应值进行填充。

468-4、返回值

        返回一个新DataFrame,其中每个元素优先选择第一个DataFrame中的非空值,如果第一个DataFrame中的值是NaN,则用第二个DataFrame中的相应值进行替代。

468-5、说明

        无

468-6、用法
468-6-1、数据准备
python">无
468-6-2、代码示例
python"># 468、pandas.DataFrame.combine_first方法
import pandas as pd
import numpy as np
# 创建两个示例DataFrame
df1 = pd.DataFrame({'A': [1, np.nan, 3], 'B': [4, np.nan, 6]})
df2 = pd.DataFrame({'A': [np.nan, 2, 5], 'B': [7, 8, np.nan]})
# 使用combine_first进行组合
result = df1.combine_first(df2)
print(result)
468-6-3、结果输出
python"># 468、pandas.DataFrame.combine_first方法
#      A    B
# 0  1.0  4.0
# 1  2.0  8.0
# 2  3.0  6.0
pandas.DataFrame.apply%E6%96%B9%E6%B3%95">469、pandas.DataFrame.apply方法
469-1、语法
python"># 469、pandas.DataFrame.apply方法
pandas.DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), by_row='compat', engine='python', engine_kwargs=None, **kwargs)
Apply a function along an axis of the DataFrame.Objects passed to the function are Series objects whose index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1). By default (result_type=None), the final return type is inferred from the return type of the applied function. Otherwise, it depends on the result_type argument.Parameters:
funcfunction
Function to apply to each column or row.axis{0 or ‘index’, 1 or ‘columns’}, default 0
Axis along which the function is applied:0 or ‘index’: apply function to each column.1 or ‘columns’: apply function to each row.rawbool, default False
Determines if row or column is passed as a Series or ndarray object:False : passes each row or column as a Series to the function.True : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction function this will achieve much better performance.result_type{‘expand’, ‘reduce’, ‘broadcast’, None}, default None
These only act when axis=1 (columns):‘expand’ : list-like results will be turned into columns.‘reduce’ : returns a Series if possible rather than expanding list-like results. This is the opposite of ‘expand’.‘broadcast’ : results will be broadcast to the original shape of the DataFrame, the original index and columns will be retained.The default behaviour (None) depends on the return value of the applied function: list-like results will be returned as a Series of those. However if the apply function returns a Series these are expanded to columns.argstuple
Positional arguments to pass to func in addition to the array/series.by_rowFalse or “compat”, default “compat”
Only has an effect when func is a listlike or dictlike of funcs and the func isn’t a string. If “compat”, will if possible first translate the func into pandas methods (e.g. Series().apply(np.sum) will be translated to Series().sum()). If that doesn’t work, will try call to apply again with by_row=True and if that fails, will call apply again with by_row=False (backward compatible). If False, the funcs will be passed the whole Series at once.New in version 2.1.0.engine{‘python’, ‘numba’}, default ‘python’
Choose between the python (default) engine or the numba engine in apply.The numba engine will attempt to JIT compile the passed function, which may result in speedups for large DataFrames. It also supports the following engine_kwargs :nopython (compile the function in nopython mode)nogil (release the GIL inside the JIT compiled function)parallel (try to apply the function in parallel over the DataFrame)Note: Due to limitations within numba/how pandas interfaces with numba, you should only use this if raw=TrueNote: The numba compiler only supports a subset of valid Python/numpy operations.Please read more about the supported python features and supported numpy features in numba to learn what you can or cannot use in the passed function.New in version 2.2.0.engine_kwargsdict
Pass keyword arguments to the engine. This is currently only used by the numba engine, see the documentation for the engine argument for more information.**kwargs
Additional keyword arguments to pass as keywords arguments to func.Returns:
Series or DataFrame
Result of applying func along the given axis of the DataFrame.
469-2、参数

469-2-1、func(必须)要应用于DataFrame的函数,可以是一个用户定义的函数或numpy的ufunc(通用函数)。

469-2-2、axis(可选,默认值为0){0 或 'index', 1 或 'columns'},确定函数应用方向,如果axis=0或'index',函数应用于每一列;如果axis=1或'columns',函数应用于每一行。

469-2-3、raw(可选,默认值为False)布尔值,如果为True,传递给函数的将是ndarray对象而不是Series对象,可能会提高性能。

469-2-4、result_type(可选,默认值为None){‘expand’, ‘reduce’, ‘broadcast’, None},确定结果的形状:

  • expand:结果将被扩展成一个DataFrame。
  • reduce:尝试减少结果的维度,例如从DataFrame到Series。
  • broadcast:保持与输入相同的形状,将结果广播回原始DataFrame。

469-2-5、args(可选,默认值为())元组,传递给func的其他位置参数。

469-2-6、by_row(可选,默认值为'compat')字符串,适用于DataFrame子类的兼容性参数,目前在Pandas中并未广泛使用。

469-2-7、engine(可选,默认值为'python'){‘cython’, ‘numba’, 'python'},指定计算引擎,'cython'和'numba' 可以用于加速计算;'python'使用默认的Python计算。

469-2-8、engine_kwargs(可选,默认值为None)字典,传递给指定计算引擎的其他关键字参数。

469-2-9、**kwargs(可选)传递给func的其他关键字参数。

469-3、功能

        将给定的函数应用于DataFrame的行或列上,可以按照行或列进行迭代,并对每一行或列应用指定的函数,该方法非常适合数据转换和清洗工作。

469-4、返回值

        取决于func的返回值和axis参数,如果func应用于列(axis=0),返回值将是一个Series对象;如果应用于行(axis=1),返回值可能是一个DataFrame或Series对象。

469-5、说明

        使用场景:

469-5-1、数据转换和清洗:在数据科学和机器学习项目中,经常需要对数据进行转换或清洗,例如处理缺失值、标准化数据或改变数据格式,该方法可以方便地对每一行或每一列应用相同的转换函数。

469-5-2、特征工程:在构建机器学习模型时,特征工程是一个关键步骤,该方法可以用于生成新的特征或转换现有的特征。

469-5-3、数据聚合和汇总:在需要对数据进行聚合操作时,该方法可以用于对每一行或每一列的值进行计算。

469-5-4、条件逻辑操作:可以根据条件对数据进行过滤或转换操作。

469-5-5复杂计算:在一些复杂的场景下,直接使用内置函数可能不够灵活或高效,此时可以使用该方法编写更为复杂的计算逻辑。

469-5-6、合并和连接操作:当需要在DataFrame内部进行多列的合并操作时,该方法也是一个常用工具。

469-6、用法
469-6-1、数据准备
python">无
469-6-2、代码示例
python"># 469、pandas.DataFrame.apply方法
# 469-1、数据转换和清洗
import pandas as pd
import numpy as np
data = {'A': [1, 2, np.nan, 4],'B': [5, np.nan, np.nan, 8],'C': [9, 10, 11, 12]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# 填充列中缺失值为列的均值
df = df.apply(lambda x: x.fillna(x.mean()), axis=0)
print("\nDataFrame after filling missing values with mean:")
print(df)# 469-2、特征工程
import pandas as pd
data = {'text': ['Python is great', 'Pandas makes things easy', 'Machine learning is fun']}
df = pd.DataFrame(data)
def extract_keywords(text):# 简单的关键词提取函数,实际使用可以考虑更复杂的方法return text.split()
df['keywords'] = df['text'].apply(extract_keywords)
print("\nDataFrame with extracted keywords:")
print(df)
data = {'date': pd.to_datetime(['2021-01-01', '2022-05-03', '2023-07-19'])}
df = pd.DataFrame(data)
df['year'] = df['date'].apply(lambda x: x.year)
df['month'] = df['date'].apply(lambda x: x.month)
df['day'] = df['date'].apply(lambda x: x.day)
print("\nDataFrame with extracted year, month, and day:")
print(df)# 469-3、数据聚合和汇总
import pandas as pd
data = {'price': [100, 250, 150],'quantity': [1, 2, 3]
}
df = pd.DataFrame(data)
df['total_spent'] = df.apply(lambda x: x['price'] * x['quantity'], axis=1)
print("\nDataFrame with total spent:")
print(df)# 469-4、条件逻辑操作
import pandas as pd
data = {'price': [70, 150, 90]
}
df = pd.DataFrame(data)
df['high_value'] = df.apply(lambda x: 'Yes' if x['price'] > 100 else 'No', axis=1)
print("\nDataFrame with high_value column based on price:")
print(df)# 469-5、复杂计算
import pandas as pd
data = {'A': [1, 2, 3],'B': [4, 5, 6],'C': [7, 8, 9]
}
df = pd.DataFrame(data)
def custom_metric(row):return (row['A'] * 2 + row['B'] ** 2) / row['C']
df['metric'] = df.apply(custom_metric, axis=1)
print("\nDataFrame with custom metric:")
print(df)# 469-6、合并和连接操作
import pandas as pd
data = {'first_name': ['John', 'Jane', 'Jim'],'last_name': ['Doe', 'Doe', 'Beam']
}
df = pd.DataFrame(data)
df['full_name'] = df.apply(lambda x: x['first_name'] + ' ' +  x['last_name'], axis=1)
print("\nDataFrame with full_name:")
print(df)
469-6-3、结果输出
python"># 469、pandas.DataFrame.apply方法
# 469-1、数据转换和清洗
# Original DataFrame:
#      A    B   C
# 0  1.0  5.0   9
# 1  2.0  NaN  10
# 2  NaN  NaN  11
# 3  4.0  8.0  12
#
# DataFrame after filling missing values with mean:
#           A    B   C
# 0  1.000000  5.0   9
# 1  2.000000  6.5  10
# 2  2.333333  6.5  11
# 3  4.000000  8.0  12# 469-2、特征工程
# DataFrame with extracted keywords:
#                        text                       keywords
# 0           Python is great            [Python, is, great]
# 1  Pandas makes things easy  [Pandas, makes, things, easy]
# 2   Machine learning is fun   [Machine, learning, is, fun]
#
# DataFrame with extracted year, month, and day:
#         date  year  month  day
# 0 2021-01-01  2021      1    1
# 1 2022-05-03  2022      5    3
# 2 2023-07-19  2023      7   19# 469-3、数据聚合和汇总
# DataFrame with total spent:
#    price  quantity  total_spent
# 0    100         1          100
# 1    250         2          500
# 2    150         3          450# 469-4、条件逻辑操作
# DataFrame with high_value column based on price:
#    price high_value
# 0     70         No
# 1    150        Yes
# 2     90         No# 469-5、复杂计算
# DataFrame with custom metric:
#    A  B  C    metric
# 0  1  4  7  2.571429
# 1  2  5  8  3.625000
# 2  3  6  9  4.666667# 469-6、合并和连接操作
# DataFrame with full_name:
#   first_name last_name full_name
# 0       John       Doe  John Doe
# 1       Jane       Doe  Jane Doe
# 2        Jim      Beam  Jim Beam
pandas.DataFrame.applymap%E6%96%B9%E6%B3%95">470、pandas.DataFrame.applymap方法
470-1、语法
python"># 470、pandas.DataFrame.applymap方法
pandas.DataFrame.applymap(func, na_action=None, **kwargs)
Apply a function to a Dataframe elementwise.Deprecated since version 2.1.0: DataFrame.applymap has been deprecated. Use DataFrame.map instead.This method applies a function that accepts and returns a scalar to every element of a DataFrame.Parameters:
func
callable
Python function, returns a single value from a single value.na_action
{None, ‘ignore’}, default None
If ‘ignore’, propagate NaN values, without passing them to func.**kwargs
Additional keyword arguments to pass as keywords arguments to func.Returns:
DataFrame
Transformed DataFrame.
470-2、参数

470-2-1、func(必须)对DataFrame中的每一个元素应用的函数。

470-2-2、na_action(可选,默认值为None)指定如何处理NaN(缺失值),默认值None表示NaN也会被传递到函数func中进行处理;'ignore'表示忽略NaN值,不对它们进行任何处理,即这些值将保持为NaN。

470-2-3、**kwargs(可选)传递给映射函数的其他参数。

470-3、功能

        将指定的函数应用于DataFrame中的每一个元素,它是逐元素操作的,因此适用于需要对DataFrame中每个元素进行相同处理的场景。

470-4、返回值

        返回一个新的DataFrame,其结构(行和列的标签)与原始DataFrame相同,但每个元素都经过了传入函数的处理。

470-5、说明

        使用场景:

470-5-1、数据清洗和预处理

470-5-1-1、批量转换数据格式:当你需要将DataFrame中的所有数据元素转换为某种格式(如将所有字符串转为小写,或将所有数字转为特定的单位)时,applymap是一个理想的选择。

470-5-1-2、数据修复:例如,将DataFrame中的所有None或空字符串转换为NaN,或者将数据中的特定字符(如$)去除。

470-5-2、特征工程

470-5-2-1、统一特征处理:在机器学习的特征工程中,常常需要对整个数据集的每个元素进行相同的转换操作,比如对每个元素进行标准化、归一化或取对数等操作。

470-5-2-2、特征衍生:可以使用applymap在数据的每个元素上执行数学运算或逻辑操作,从而生成新的特征。

470-5-3、字符串操作

470-5-3​​​​​​​-1、批量处理字符串:如果DataFrame中的每个元素都是字符串,可以使用applymap来执行各种操作,如去除空格、替换字符、格式转换等。

470-5-4、数值计算

470-5-4-1、数学变换:如果需要对数据中的每个数值元素进行同样的数学变换,比如乘法、指数计算、对数计算等,applymap可以帮助你快速实现。

470-5-4-2、异常值处理:可以使用applymap识别和处理异常值,比如将所有超过某个阈值的元素替换为NaN或其他值。

470-5-5、条件逻辑

470-5-5​​​​​​​-1、条件转换:你可以使用applymap根据某些条件对DataFrame中的每个元素进行转换,比如将所有负数替换为0,或者根据某些条件将元素分组。

470-5-6、数据格式化

470-5-6-1、输出格式调整:当需要对DataFrame中的数值进行格式化,比如将所有数值显示为两位小数,或者将布尔值转换为字符串"True"/"False"时,applymap能够有效实现这一点。

470-5-7、数据可视化前的准备

470-5-7-1、格式调整:在将DataFrame数据用于可视化之前,可能需要对数据进行格式化或转换,使其更易于解读或符合图表的要求,这时applymap也非常有用。

470-6、用法
470-6-1、数据准备
python">无
470-6-2、代码示例
python"># 470、pandas.DataFrame.applymap方法
# 470-1、数据清洗和预处理
# 470-1-1、批量转换数据格式
import pandas as pd
df = pd.DataFrame({'Name': ['Myelsa', 'Bryce', 'Jimmy'],'City': ['New York', 'Los Angeles', 'Chicago']
})
# 将所有字符串转换为小写
df = df.applymap(lambda x: x.lower() if isinstance(x, str) else x)
print(df)# 470-1-2、数据修复
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, None, 3],'B': ['x', None, 'z']
})
# 将None替换为NaN
df = df.applymap(lambda x: np.nan if x is None else x)
print(df)# 470-2、特征工程
import pandas as pd
import numpy as np
df = pd.DataFrame({'Feature1': [1, 10, 100],'Feature2': [0.5, 5, 50]
})
# 对每个元素取对数
df = df.applymap(lambda x: np.log(x))
print(df)# 470-3、字符串操作
import pandas as pd
df = pd.DataFrame({'A': ['  hello', 'world  ', '  pandas  '],'B': ['  data  ', 'science  ', '  rocks!  ']
})
# 去除字符串两边的空格
df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
print(df)# 470-4、数值计算
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3],'B': [4, 5, 6]
})
# 每个元素乘以10
df = df.applymap(lambda x: x * 10)
print(df)# 470-5、条件逻辑
import pandas as pd
df = pd.DataFrame({'A': [1, -2, 3],'B': [-4, 5, -6]
})
# 将负数替换为0
df = df.applymap(lambda x: 0 if x < 0 else x)
print(df)# 470-6、数据格式化
import pandas as pd
df = pd.DataFrame({'A': [1.2345, 2.3456, 3.4567],'B': [4.5678, 5.6789, 6.7890]
})
# 将数值格式化为两位小数
df = df.applymap(lambda x: f"{x:.2f}")
print(df)# 470-7、数据可视化前的准备
import pandas as pd
df = pd.DataFrame({'A': [True, False, True],'B': [False, True, False]
})
# 将布尔值转换为字符串
df = df.applymap(lambda x: "True" if x else "False")
print(df)
470-6-3、结果输出
python"># 470、pandas.DataFrame.applymap方法
# 470-1、数据清洗和预处理
# 470-1-1、批量转换数据格式
#      Name         City
# 0  myelsa     new york
# 1   bryce  los angeles
# 2   jimmy      chicago# 470-1-2、数据修复
#      A    B
# 0  1.0    x
# 1  NaN  NaN
# 2  3.0    z# 470-2、特征工程
#    Feature1  Feature2
# 0  0.000000 -0.693147
# 1  2.302585  1.609438
# 2  4.605170  3.912023# 470-3、字符串操作
#         A        B
# 0   hello     data
# 1   world  science
# 2  pandas   rocks!# 470-4、数值计算
#     A   B
# 0  10  40
# 1  20  50
# 2  30  60# 470-5、条件逻辑
#    A  B
# 0  1  0
# 1  0  5
# 2  3  0# 470-6、数据格式化
#       A     B
# 0  1.23  4.57
# 1  2.35  5.68
# 2  3.46  6.79# 470-7、数据可视化前的准备
#        A      B
# 0   True  False
# 1  False   True
# 2   True  False

二、推荐阅读

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

http://www.ppmy.cn/news/1517578.html

相关文章

bash 脚本的执行方式

在 Bash 中&#xff0c;执行脚本可以通过多种方式&#xff0c;其中最常见的两种是使用 bash 直接运行脚本和使用 source 命令&#xff08;或 .&#xff0c;即点命令&#xff09;来执行。这两种方式有着根本的区别&#xff0c;它们对脚本的影响和脚本对环境的影响也不同。 使用…

解析云原生架构中两大核心原则

1.云原生架构是什么 云原生架构是一种设计和构建应用程序的现代方法&#xff0c;以微服务、容器化、持续集成和持续部署&#xff08;CI/CD&#xff09;等技术为基础&#xff0c;使应用能够在云环境中动态运行。云原生架构强调解耦合、弹性和自动化&#xff0c;开发团队在独立的…

vue中video视频路径改变,dom不更新问题

效果展示 视频切换前 视频切换后 完整代码 <template><!-- 设置v-if只在路径有值时&#xff0c;标签才存在 --><video v-if"state.videoSrc ! null" controls><source :src"state.videoSrc" type"video/mp4"></…

苹果裁员与服务战略调整:科技巨头应对市场挑战的新动向

引言 近期&#xff0c;苹果公司的一系列动态引发了业界的广泛关注。从秋季发布会的定档到高层人事变动&#xff0c;再到服务部门的裁员决策&#xff0c;苹果正以前所未有的姿态调整其业务布局&#xff0c;以应对日益激烈的市场竞争和技术变革。 一、服务部门裁员&#xff1a;战…

第六章 rust中的错误处理

本系列文章已升级、转移至我的自建站点中,本章原文为:rust中的错误处理 目录 一、前言二、不同之处三、枚举处理错误四、泛型五、标准错误处理1.Result2.Option六、panic、传递错误七、常见用法八、为Enum绑定方法一、前言 本章将要介绍的是Rust中的错误处理方式,这是一个与…

别错过!24年软考科目精选指南,你选对了吗?

近年来&#xff0c;软考&#xff08;软件水平考试&#xff09;的难度逐渐攀升&#xff0c;这并非源于题目本身的复杂化&#xff0c;而是官方对通过率的调控策略所致。整体通过率维持在13%左右&#xff0c;高级别考试更是低至10%以下&#xff0c;考生需慎重对待。以湖南2024年上…

【Rust光年纪】提升Rust文件操作效率:探秘6大利器

高效处理I/O任务&#xff1a;Rust文件系统操作利器大揭秘 前言 在Rust编程语言中&#xff0c;文件操作和I/O处理一直是开发者们关注的重点。为了满足不同需求&#xff0c;出现了许多优秀的库和工具。本文将介绍几个在Rust中用于文件锁、文件系统辅助、内存映射文件操作、事件…

linux系统中内存和缓冲简介

在Linux系统中&#xff0c;内存管理是一个复杂的过程&#xff0c;涉及到多种类型的内存使用。mem和buff通常指的是内存使用的不同方面&#xff1a; 1. **Mem**&#xff1a;通常指的是物理内存&#xff08;RAM&#xff09;的使用情况。在Linux系统中&#xff0c;/proc/meminfo文…

opencv开发环境搭建

从github下载opencv最新源码https://github.com/opencv/opencv,目前最新是5e93c8202363a13fc72df30f8c14069c5ab66e42. Ubuntu环境下编译 安装依赖库: sudo apt-get install build-essentialsudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavf…

【高级IO总结】深度探索高级IO:五种IO模型、高级IO、Select、Poll、Epoll工作模式

文章目录 前言文章链接 前言 IO 是 “Input/Output”&#xff08;输入/输出&#xff09; 的缩写&#xff0c;指的是计算机系统与外部世界之间的数据传输和交换过程。 网络通信的本质就是&#xff1a;IO 总体来说&#xff0c;IO的效率是相对较低的&#xff0c;为什么&#xf…

python爬虫:selenium+browsermobproxy实现浏览器请求抓取(模块安装详解)

前言 本来很多场景用beautiful和requests就能解决的&#xff0c;但是最近发现了某些网站会使用<link>来链接网页信息&#xff0c;让我没办法通过requests获取页面的具体内容&#xff1b;并且接口也加入了某种token的生成方案&#xff0c;导致我无从下手。 因此&#xff0…

P2234 [HNOI2002] 营业额统计 java版本

文章目录 P2234 [HNOI2002] 营业额统计 java版本题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 提示算法分析代码实现结语 P2234 [HNOI2002] 营业额统计 java版本 题目描述 Tiger 最近被公司升任为营业部经理&#xff0c;他上任后接受公司交给的第一项任务便是统计…

程序猿成长之路之数据挖掘篇——Kmeans聚类算法

Kmeans 是一种可以将一个数据集按照距离&#xff08;相似度&#xff09;划分成不同类别的算法&#xff0c;它无需借助外部标记&#xff0c;因此也是一种无监督学习算法。 什么是聚类 用官方的话说聚类就是将物理或抽象对象的集合分成由类似的对象组成的多个类的过程。用自己的…

【在Linux世界中追寻伟大的One Piece】传输层协议UDP

目录 1 -> 传输层 2 -> 端口号 2.1 -> 端口号范围划分 2.2 -> 知名端口号 3 -> UDP协议 3.1 -> UDP协议端格式 3.2 -> UDP的特点 3.2.1 -> 面向数据报 3.3 -> UDP的缓冲区 3.4 -> UDP使用注意事项 3.5 -> 基于UDP的应用层协议 1 -…

基于SSM+小程序的旅游社交登录管理系统(旅游4)(源码+sql脚本+视频导入教程+文档)

&#x1f449;文末查看项目功能视频演示获取源码sql脚本视频导入教程视频 1 、功能描述 本旅游社交小程序功能有管理员和用户。管理员有个人中心&#xff0c;用户管理&#xff0c;每日签到管理&#xff0c;景点推荐管理&#xff0c;景点分类管理&#xff0c;防疫查询管理&…

力扣——数组(一)

一、二分法&#xff08;有序数组&#xff09; 1、搜索等于target的元素 法一&#xff1a; 直接遍历 class Solution { public:int search(vector<int>& nums, int target) {int i0;for(i0;i<nums.size();i){if(nums[i]target){return i;}}return -1;} }; 法二…

【C++第十五章】继承

【C第十五章】继承 定义&#x1f9d0; 继承是C面向对象编程中的一个核心概念&#xff0c;它允许创建一个新类&#xff08;称为派生类或子类&#xff09;从已有类&#xff08;称为基类或父类&#xff09;中继承属性和方法。 继承的主要用途包括&#xff1a; 代码重用&#xff1…

U盘读不出来怎么办

目录 一、检查物理连接 1. 重新插拔U盘 2. 检查U盘外观 二、软件设置检查 1. 取消隐藏U盘 2. 更新或重新安装U盘驱动 3. 检查磁盘管理 三、文件系统修复 1. 格式化U盘 2. 使用命令提示符修复 四、病毒扫描 五、其他注意事项 一、检查物理连接 1. 重新插拔U盘 最简单也…

RTC相关

RTC唤醒 &#xff08;Real Time Clock) sudo rtcwake -m [mode] -s [seconds]-m 选项指定进入的电源管理模式&#xff0c;可以是&#xff1a; standby&#xff1a;进入待机模式 freeze&#xff1a;冻结模式 mem&#xff1a;挂起到内存 disk&#xff1a;挂起到磁盘 off&#xf…

AI产品经理学习路线【2024最新】,从零基础到精通,非常详细收藏我这一篇就够了

成为一名优秀的AI产品经理不仅需要掌握相关的技术知识&#xff0c;还需要具备良好的产品思维、市场洞察力以及跨部门沟通协调能力。下面是一个详细的AI产品经理学习路线&#xff0c;旨在帮助有志于从事该职业的人士快速成长。 AI产品经理的学习路线 第一阶段&#xff1a;基础知…