【问题标题】:PerformanceWarning: DataFrame is highly fragmented While Using Pandas Assign functionPerformanceWarning:DataFrame 在使用 Pandas Assign 函数时高度分散
【发布时间】:2022-11-16 03:13:31
【问题描述】:

我正在尝试将字典添加到现有的熊猫数据框中 这就是代码的样子

features_dict = {"feature1":0,"feature2":0, ......, "featureN":1}
feature_df = feature_df.assign(**features_dict)

现在,字典中的键数超过了 100 个,需要将它们全部添加为一列。 当我运行此代码时,我收到以下警告

PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead

什么应该是更好的方法来做到这一点?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您是否按照这个友好 (!) 警告的建议尝试过pandas.concat?

    feature_df = pd.concat([feature_df, pd.DataFrame(features_dict)], axis=1)
    

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2021-09-18
      • 2022-01-27
      • 1970-01-01
      • 2021-04-13
      • 2022-01-12
      • 1970-01-01
      • 2020-05-04
      • 2022-11-23
      相关资源
      最近更新 更多