Python 我需要用一些组替换数据框中列中的值

Python 我需要用一些组替换数据框中列中的值,python,pandas,dataframe,Python,Pandas,Dataframe,问题表述 目前,df['B']的值范围为0到16。我希望将其替换为以下内容: S where df['B'] <= 2 M where 2 < df['B'] <= 6 L where df['B'] >= 6 所需输出 有熊猫函数可以这样做吗?我假设您的数据帧的名称是df df['B'] = pd.cut(df['B'], bins = [0, 3, 7, 17], labels=["S", "M", "L"]) 切割垃圾箱意味着: 我们需要的样本数据

问题表述

目前,
df['B']
的值范围为0到16。我希望将其替换为以下内容:

  S where df['B'] <= 2
  M where 2 < df['B'] <= 6
  L where df['B'] >= 6

所需输出


有熊猫函数可以这样做吗?

我假设您的数据帧的名称是
df

df['B'] = pd.cut(df['B'], bins = [0, 3, 7, 17], labels=["S", "M", "L"])
切割垃圾箱意味着:


我们需要的样本数据可能重复,我们需要至少1question@Shiven辛格:这是一个基本的装箱操作,上面的链接答案会有所帮助
A       B
5180    S
5784    S
5784    L
7269    M
7268    L
df['B'] = pd.cut(df['B'], bins = [0, 3, 7, 17], labels=["S", "M", "L"])
for range [0,3) we use label[0] ('S')  

for range [3,7) we use label[1] ('M')

for range [7,17) we use label[2] ('L')