【问题标题】:Split columns in dataframe into two new dataframes将数据框中的列拆分为两个新的数据框
【发布时间】:2018-06-21 11:44:52
【问题描述】:

我在一个数据框中有数据,我在一个单元格中有两个观察结果:

                          small             medium        large
apples                258 0.12%         39 0.0091%     89 0.18%
carrots                97 0.16%          6  0.012%     26 0.26%
bananas               377 0.14%         12  0.018%    128 0.22%
pears                 206 0.17%          7  0.034%    116 0.24%

我想创建两个单独的数据框来拆分观察结果。像这样的:

                    small           medium          large
apples                258               39             89
carrots                97                6             26
bananas               377               12            128
pears                 206                7            116

第二个:

                      small             medium        large
apples                0.12%            0.0091%        0.18%
carrots               0.16%             0.012%        0.26%
bananas               0.14%             0.018%        0.22%
pears                 0.17%             0.034%        0.24%

我可以逐列拆分:

 new_df1 = df['small'].str.extract('([^\s]+)', expand=True)
 new_df2 = df['small'].str.extract('([^\s]*$)', expand=True)

但我不知道如何为整个 DataFrame 执行此操作。我有许多类似的数据框,具有不同的列名和行名,因此我正在寻找可以重用的解决方案。谢谢!

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    你可以这样做:

    df1 = df.applymap(lambda x: x.split()[0])
    df2 = df.applymap(lambda x: x.split()[1])
    

    例子df:

       small medium
    0  0 33%  0 33%
    1  1 44%  1 33%
    2  2 55%  1 55%
    

    df1:

     small medium
    0  0   0
    1  1   1
    2  2   1
    

    df2:

      small medium
    0  33%  33%
    1  44%  33%
    2  55%  55%
    

    【讨论】:

    • 请注意,您生成的数据帧将具有object 类型的系列。您应该转换为数字(例如我的回答)以避免失去性能。否则,您的数据框将只是一个指针集合。
    【解决方案2】:

    使用pd.DataFrame.applymap 并通过operator.itemgetter 提取每个组件:

    from operator import itemgetter
    
    df = pd.DataFrame([['258 0.12%', '39 0.0091%', '89 0.18%'],
                       ['97 0.16%', '6  0.012%', '26 0.26%']],
                      columns=['small', 'medium', 'large'],
                      index=['apples', 'carrots'])
    
    split = df.applymap(lambda x: x.split())
    
    df1 = split.applymap(itemgetter(0)).astype(int)
    df2 = split.applymap(lambda x: x[1][:-1]).astype(float) / 100
    

    请注意,您必须注意将字符串分别转换为 intfloat

    print(df1)
    
             small  medium  large
    apples     258      39     89
    carrots     97       6     26
    
    print(df2)
    
              small    medium   large
    apples   0.0012  0.000091  0.0018
    carrots  0.0016  0.000120  0.0026
    

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 2022-01-09
      • 2021-06-18
      • 1970-01-01
      • 2020-09-22
      • 2022-01-20
      • 1970-01-01
      • 2021-10-11
      相关资源
      最近更新 更多