【问题标题】:python performance problems while classifing column values分类列值时的python性能问题
【发布时间】:2017-12-17 13:05:56
【问题描述】:

这个问题与我之前的问题密切相关: here
抱歉,我又要问了!

下面的代码正在运行并提供正确的结果,但它再次有点慢(80K 行需要 4 分钟)。我在将 Pandas 的 Series 类用于具体值时遇到问题。有人可以推荐我如何对这些列进行分类吗?

在纪录片中找不到相关信息:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html

运行代码:

# p_test_SOLL_test_D10

for x in range (0,len(tableContent[6])):
    var = tableContent[6].loc[x, ('p_test_LAENGE')]
    if float(tableContent[6].loc[x, ('p_test_LAENGE')])>=100.0:
        tableContent[6].loc[x, ('p_test_LAENGE')]='yes'
    elif (float(tableContent[6].loc[x, ('p_test_LAENGE')]) <30.0 and float(tableContent[6].loc[x, ('p_test_LAENGE')]) >= 10):
        tableContent[6].loc[x, ('p_test_LAENGE')]='yes2'
    elif (float(tableContent[6].loc[x, ('p_test_LAENGE')]) <10.0 and float(tableContent[6].loc[x, ('p_test_LAENGE')]) >= 5):
        tableContent[6].loc[x, ('p_test_LAENGE')]='yes3'
    else:
        tableContent[6].loc[x, ('p_test_LAENGE')]='no'

print (tableContent[6]['p_test_LAENGE'])

系列尝试:

if tableContent[6]['p_test_LAENGE'].astype(float) >=100.0:
    tableContent[6]['p_test_LAENGE']='yes'
elif (tableContent[6]['p_test_LAENGE'].astype(float) <30.0 and tableContent[6]['p_test_LAENGE'].astype(float) >= 10):
    tableContent[6]['p_test_LAENGE']='yes1'
elif (tableContent[6]['p_test_LAENGE'].astype(float) <10.0 and tableContent[6]['p_test_LAENGE'].astype(float) >= 5):
    tableContent[6]['p_test_LAENGE']='yes2'
else:
    tableContent[6]['p_test_LAENGE']='no'


print (tableContent[6]['p_test_LAENGE'])

【问题讨论】:

  • 有更多经验丰富的人使用 Pandas(可能现在正在看这个问题)。所以,我什至不会尝试给你一个规范的方法。但是,您上一个问题中的建议提到了“矢量化”和“取消循环”。如果您使用for 循环,它们大约在Python 时间内运行(计算上),您最好不要为numpypandas 而烦恼,只需使用香草python。它需要转变思维,在继续之前,这里有几个教程可能适合您。

标签: python pandas data-science-experience


【解决方案1】:

我没有你的df 来测试,所以你需要修改下面的代码。 假设df 的最小值大于10e-7df 的最大值小于10e7

bin = [10e-7,5,10,30,100,10e7]
label = ['no','yes2','yes1','no','yes']
df['p_test_LAENGE_class'] = pd.cut(df['p_test_LAENGE'], bins=bin, labels=label)

希望对你有帮助

【讨论】:

  • 谢谢!切割方法是我一直在寻找的!
猜你喜欢
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多