python - Pandas 申请() : how to multiply selected columns based on string match and return complete dataframe

标签 python pandas apply

我有姓名、FDR%、年龄、FCR%、收入等列。我想选择与“%”字符串匹配的列并乘以 100。最后,我想返回带有“%”的整个数据帧列值发生变化。我尝试如下:

df_final=df_1.filter(like='%', axis=1).apply(lambda x:x*100)
df_final

这仅返回子集,即根据 FDR% 和 FCR% 操作的列。我需要返回带有相应更改的整个数据帧。

还有更好的方法来实现同样的目的吗?

最佳答案

您可以从 filter 返回的 DataFrame 中选择列,并按 100 倍增:

df_1 = pd.DataFrame({
        'A':list('abcdef'),
         'FDR%':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'FCR%':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
})

cols = df_1.filter(like='%').columns
df_1[cols] *= 100
print (df_1)
   A  FDR%  C  FCR%  E
0  a   400  7   100  5
1  b   500  8   300  3
2  c   400  9   500  6
3  d   500  4   700  9
4  e   500  2   100  2
5  f   400  3     0  4

或者使用 Series.str.contains 的掩码或Series.str.endswith并选择带有 DataFrame.loc 的列:

mask = df_1.columns.str.contains('%')
#alternative
#mask = df_1.columns.str.endswith('%')
df_1.loc[:, mask] *= 100

通知:

apply 在这里对于多个是不好的选择,因为在引擎盖下循环,所以很慢。快速解决方案仅乘以标量。

关于python - Pandas 申请() : how to multiply selected columns based on string match and return complete dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56037030/

相关文章:

python - 大型 python tornado 项目的最佳结构是什么?

python - 如何用逗号格式化 float 作为 f 字符串中的小数分隔符?

python - 为什么这个脚本需要这么长时间才能运行?

python - 减去 Pandas 数据框

python - 如何使用 pandas groupby 并一起移动

python - 使用 Python 进行简单的远程进程监控

javascript - 来自 python 列表的数据列表

python - 值错误: time data '10/11/2006 24:00' does not match format '%d/%m/%Y %H:%M'

pandas - Polars 将性能应用于自定义功能

r - 从数据帧列表中获取名称与 id 连接的向量