python pandas - 将值输入到新列中

标签 python pandas

下面我有一个 4 人消费的小数据框。 有一个名为“等级”的空列。 我想将花费超过 100 美元的人评为 A 级,将花费少于 100 美元的人评为 B 级。 假设它是一个大数据框,填充“等级”列的最有效方法是什么?

import pandas as pd
df=pd.DataFrame({'Customer':['Bob','Ken','Steve','Joe'],
             'Spending':[130,22,313,46]})
df['Grade']=''

enter image description here

最佳答案

您可以使用numpy.where :

df['Grade']= np.where(df['Spending'] > 100 ,'A','B')
print (df)
  Customer  Spending Grade
0      Bob       130     A
1      Ken        22     B
2    Steve       313     A
3      Joe        46     B

时间:

df=pd.DataFrame({'Customer':['Bob','Ken','Steve','Joe'],
             'Spending':[130,22,313,46]})

#[400000 rows x 4 columns]
df = pd.concat([df]*100000).reset_index(drop=True)

In [129]: %timeit df['Grade']= np.where(df['Spending'] > 100 ,'A','B')
10 loops, best of 3: 21.6 ms per loop

In [130]: %timeit df['grade'] = df.apply(lambda row: 'A' if row['Spending'] > 100 else 'B', axis = 1)
1 loop, best of 3: 7.08 s per loop

关于python pandas - 将值输入到新列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41165818/

相关文章:

python - 在 Python 运行时,有没有办法区分文字字符串实例和动态创建的实例?

python - 为什么带有 Python 扩展的 Visual Studio Code 无法正确导入 "fractions"库?

python - 如何在图表中运行 elif 函数?

python - 时间序列数据中的 ValueError

python - 将带有字符串的结构化 numpy 数组传递给 cython 函数

python - UnsatisfiableError : The following specifications were found to be in conflict: conda 4. 0.8* -> conda-env <2.5 -> python 2.7* - python 3.6*

python - 从主进程和派生进程使用 matplotlib

python - 删除出现次数超过 N 次的重复值

python - 如何按频率对行进行分组?

python - Pandas - update() 创建 "random"值?