简体   繁体   English

为什么我不能在for循环中直接操作pandas DataFrame?

[英]Why can't I directly manipulate a pandas DataFrame in a for loop?

full_data = [train, test]
for dataset in full_data:
     dataset = pd.get_dummies(dataset)

I am trying to do one-hot-encoding of both my train & test pandas DataFrames. 我正在尝试对火车和测试熊猫DataFrame进行一次热编码。 It turned out that neither the train or the test DataFrame was changed. 事实证明,火车或测试DataFrame均未更改。 Could I get some help here on why this does not work? 我可以在这里得到一些帮助,以了解为什么它不起作用吗? Thanks a lot! 非常感谢!

You need: 你需要:

full_data = pd.concat([train,test])

df_new = pd.get_dummies(full_data, 
                        columns=categorical_columns, 
                        drop_first=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM