本文介绍了 pandas :根据更复杂的标准选择和修改数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看线程,尽管我的问题并没有太大不同,但还是有一些区别.我有一个完整的floats数据框,我想用字符串替换.说:

I was looking at this and this threads, and though my question is not so different, it has a few differences. I have a dataframe full of floats, that I want to replace by strings. Say:

      A     B       C
 A    0     1.5     13
 B    0.5   100.2   7.3
 C    1.3   34      0.01

对于此表,我想用几个条件替换,但只有第一个替换可以使用:

To this table I want to replace by several criteria, but only the first replacement works:

df[df<1]='N' # Works
df[(df>1)&(df<10)]#='L' # Doesn't work
df[(df>10)&(df<50)]='M'  # Doesn't work
df[df>50]='H'  # Doesn't work

如果我改为根据float对第二行进行选择,则仍然不起作用:

If I instead do the selection for the 2nd line based on float, still doesn't work:

((df.applymap(type)==float) & (df<10) & (df>1)) #Doesn't work

我想知道如何在这里或其他任何方式应用pd.DataFrame().mask.我该怎么解决?

I was wondering how to apply pd.DataFrame().mask in here, or any other way. How should I solve this?

或者,我知道我可以逐列阅读并将替换应用于每个系列,但这似乎适得其反

Alternatively, I know I may read column by column and apply the substitutions on each series, but this seems a bit counter productive

谁能解释为什么上面的4个简单作业不起作用?

Could anyone explain why the 4 simple assignments above do not work?

推荐答案

您可以使用 searchsorted

You can use searchsorted

labels = np.array(list('NLMH'))
breaks = np.array([1, 10, 50])
pd.DataFrame(
    labels[breaks.searchsorted(df.values)].reshape(df.shape),
    df.index, df.columns)

   A  B  C
A  N  L  M
B  N  H  L
C  L  M  N


就地

labels = np.array(list('NLMH'))
breaks = np.array([1, 10, 50])
df[:] = labels[breaks.searchsorted(df.values)].reshape(df.shape)
df

   A  B  C
A  N  L  M
B  N  H  L
C  L  M  N


pandas.DataFrame.mask

束缚纯熊猫方法

从0.21版开始弃用


Chained pure Pandas approach with pandas.DataFrame.mask

Deprecated since version 0.21

df.mask(df.lt(1), 'N').mask(df.gt(1) & df.lt(10), 'L') \
  .mask(df.gt(10) & df.lt(50), 'M').mask(df.gt(50), 'H')

   A  B  C
A  N  L  M
B  N  H  L
C  L  M  N

这篇关于 pandas :根据更复杂的标准选择和修改数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:31