【问题标题】:New pandas dataframe from meta information of existing DF来自现有 DF 元信息的新 pandas 数据框
【发布时间】:2015-07-09 14:30:56
【问题描述】:

目前有一个 CSV 文件,其输出日期帧如下:

[in]
df = pd.read_csv(file_name)
df.sort('TOTAL_MONTHS', inplace=True)
print df[['TOTAL_MONTHS','COUNTEM']]

[out] 
    TOTAL_MONTHS       COUNTEM
    12                 0 
    12                 0 
    12                 2 
    25                 10
    25                 0 
    37                 1
    68                 3

我想获取 'COUNTEM' 值落在预设 bin 内的总行数(按 TOTAL_MONTHS)。

数据将通过 excel/powerpoint 输入直方图:

X 轴 = 合约数量

Y 轴 = Total_months

条形颜色 = COUNTEM

图表的输入是这样的(列是COUNTEM bins):

MONTHS    0    1-3    4-6    7-10    10+    20+
0         0    0      0      0       0      0  
1         0    0      0      0       0      0   
2         0    0      0      0       0      0
3         0    0      0      0       0      0
...
12        2    1      0      0       0      0
...
25        1    0      0      0       1      0
...
37        0    1      0      0       0      0
...
68        0    1      0      0       0      0

理想情况下,我希望代码以该格式输出数据帧。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    有趣的问题。了解熊猫(因为我不正确)可能会有一个更漂亮和更简单的解决方案。但是,也可以通过以下方式进行迭代:

    #First, imports and create your data
    import pandas as pd
    
    DF = pd.DataFrame({'TOTAL_MONTHS'   : [12, 12, 12, 25, 25, 37, 68], 
                       'COUNTEM'        : [0, 0, 2, 10, 0, 1, 3]
                       })
    
    #Next create a data frame of 'bins' with the months as index and all
    #values set at a default of zero
    New_DF = pd.DataFrame({'bin0'   : 0,
                           'bin1'   : 0,
                           'bin2'   : 0,
                           'bin3'   : 0,
                           'bin4'   : 0,
                           'bin5'   : 0}, 
                           index = DF.TOTAL_MONTHS.unique())
    
    In [59]: New_DF
    Out[59]: 
        bin0  bin1  bin2  bin3  bin4  bin5
    12     0     0     0     0     0     0
    25     0     0     0     0     0     0
    37     0     0     0     0     0     0
    68     0     0     0     0     0     0
    
    #Create a list of bins (rather than 20 to infinity I limited it to 100)
    bins = [[0], range(1, 4), range(4, 7), range(7, 10), range(10, 20), range(20, 100)]
    
    #Now iterate over the months of the New_DF index and slice the original
    #DF where TOTAL_MONTHS equals the month of the current iteration. Then
    #get a value count from the original data frame and use integer indexing
    #to place the value count in the appropriate column of the New_DF:
    
    for month in New_DF.index:
        monthly = DF[DF['TOTAL_MONTHS'] == month]
        counts = monthly['COUNTEM'].value_counts()
        for count in counts.keys():
            for x in xrange(len(bins)):
                if count in bins[x]:
                    New_DF.ix[month, x] = counts[count]
    

    这给了我:

    In [62]: New_DF
    Out[62]: 
        bin0  bin1  bin2  bin3  bin4  bin5
    12     2     1     0     0     0     0
    25     1     0     0     0     1     0
    37     0     1     0     0     0     0
    68     0     1     0     0     0     0
    

    这似乎是您想要的。您可以根据需要重命名索引....

    希望这会有所帮助。也许有人有一个使用内置 pandas 函数的解决方案,但目前这似乎可行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2020-01-20
      • 2019-01-27
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多