python - pandas dataframe如何进行大规模的赋值?
PHP中文网
PHP中文网 2017-04-18 09:25:40
0
1
899

小弟新手,现在在处理一个二百多万的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中文网

认证高级PHP讲师

reply all(1)
刘奇

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!