search

Home  >  Q&A  >  body text

python - pandas dataframe如何进行大规模的赋值?

小弟新手,现在在处理一个二百多万的dataset,把他读取成pandas的dataframe后想根据每列特征值的不同做一个分类赋值。可是直接用datafrane做起来很慢,一个数据点都需要两三秒左右。求问有没有什么快捷的办法处理?谢谢!

比如:

                      price post_code  year  month property_type whether_new  \
0                 0   155000   N20 9AQ  1995      1             F           Y   
1                 1    67500   SE1 4HG  1995      1             F           Y   
2                 2    99950   SE1 0RD  1995      1             S           Y   
3                 3   102000   N3  1AE  1995      1             F           N   
4                 4   100000   W11 1PN  1995      1             S           N   
5                 5    57500   SW4 0NU  1995      1             F           N   

我想把property type里的F全换成'000',s全换成'001'.

PHP中文网PHP中文网2807 days ago924

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:27:40

    I saw your data, my place is very fast, it takes less than 1 second to replace all the data:

    import pandas as pd
    df = pd.read_csv('./data.csv')
    df.property_type = df.property_type.map({'F': '000', 'S': '001'})

    data.csv contains two million records, which I randomly generated

    reply
    0
  • Cancelreply