【发布时间】:2018-02-02 06:16:07
【问题描述】:
您好,我有一个看起来像这样的 pandas DataFrame:
Product_Code W0 W1 W2 W3 W4 W5 W6 W7 W8 ... \
806 P815 0 0 1 0 0 2 1 0 0 ...
807 P816 0 1 0 0 1 2 2 6 0 ...
808 P817 1 0 0 0 1 1 2 1 1 ...
809 P818 0 0 0 1 0 0 0 0 1 ...
810 P819 0 1 0 0 0 0 0 0 0 ...
Normalized 42 Normalized 43 Normalized 44 Normalized 45 \
806 0.00 0.33 0.33 0.00
807 0.43 0.43 0.57 0.29
808 0.50 0.00 0.00 0.50
809 0.00 0.00 0.00 0.50
810 0.00 0.00 0.00 0.00
但实际上我不需要这些列,我只需要 W0 和 W4,所以我想删除所有这些列,所以这是我尝试的:
raw_data = [ raw_data.drop( [i], 1, inplace = True ) for i in raw_data if i is not 'W0' and i is not 'W4' ]
半小时后,我发现由于某种原因 != 不起作用字符串 and I was wondering why? 所以我有一个稳定的解决方案:
#WORKS !!!!
# for i in raw_data:
# if i != 'W0' and i != 'W4':
# raw_data.drop( [i], 1, inplace = True )
但我一点也不喜欢它,我评论了它,因为它占用很多空间而且它不漂亮,我想让表达式工作的单行循环,是否可能,问题是那:
raw_data = [ raw_data.drop( [i], 1, inplace = True ) for i in raw_data if i != 'W0' and i != 'W4' ]
尝试将DataFrame转为列表,应该怎么做?
【问题讨论】:
标签: python pandas scikit-learn sklearn-pandas