Python Pandas用缺少的值填充数据帧

时间:2017-06-23 00:55:14

标签: python pandas

我将此数据框作为示例

  ISO Product  Billed Week  Created Week  Billings
0  DE   Table       201705        201703         0
1  AT   Table       201708        201707         0

我需要做的是用['ISO','产品']为每个组填写一些缺少0个Billings的数据,其中序列中断,即在某个星期没有创建账单所以它是失踪。它需要基于“开单周”和“最短创建周”的最大值。即这是应该完成但没有按顺序中断的组合。

因此,对于上述内容,我需要以编程方式附加到数据库中的缺失记录如下所示:

       var app = angular.module('app', []);

        app.controller('viewCtrl', function($scope, $http) {
            var url = "https://";
            $http({
                method: "GET",
                url: url,
                headers: {
                    "accept": "application/json;odata=verbose"
                }
            }).success(function(data, status, headers, config) {
                $scope.contacts = data.d.results;
                console.log($scope.contacts);
            }).error(function(data, status, headers, config) {});





        });


        app.controller('addItemsController', function($scope, $http) {
         
                var url = "https://";

                $scope.addContact = function() {
                    return $http({
                        headers: {
                            "Accept": "application/json; odata=verbose",
                            "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
                        },
                        method: "POST",
                        url: url,
                        data: {
                            'Lastname': $scope.Lastname,
                            'Firstname': $scope.Firstname
                        }
                    })
                        .then(saveContact)
                        .catch(function(message) {
                            console.log("addContact() error: " + message);
                        });

                    function saveContact(data, status, headers, config) {
                        alert("Item Added Successfully");
                        return data.data.d;
                    }
                }
                //console.log("an item has been added!");
            });

        app.controller('editItemsController', function($scope) {

            $scope.editItem = function() {
                console.log("an item can now be edited!");

            }

        });


        app.controller('deleteItemsController', function($scope) {
            $scope.deleteItem = function() {

                console.log("item has been deleted");
            }

        });

1 个答案:

答案 0 :(得分:3)

这是我的解决方案。我相信一些天才将提供更好的解决方案〜让我们等待它〜

df1=df.groupby('ISO').agg({'Billed Week' : np.max,'Created Week' : np.min})
df1['ISO']=df1.index

     Created Week  Billed Week ISO
ISO                               
AT         201704       201708  AT
DE         201701       201705  DE

ISO=[]
BilledWeek=[]
CreateWeek=[]
for i in range(len(df1)):
    BilledWeek.extend([df1.ix[i,1]]*(df1.ix[i,1]-df1.ix[i,0]+1))
    CreateWeek.extend(list(range(df1.ix[i,0],df1.ix[i,1]+1)))
    ISO.extend([df1.ix[i,2]]*(df1.ix[i,1]-df1.ix[i,0]+1))
DF=pd.DataFrame({'BilledWeek':BilledWeek,'CreateWeek':CreateWeek,'ISO':ISO})
Target=DF.merge(df,left_on=['BilledWeek','CreateWeek','ISO'],right_on=['Billed Week','Created Week','ISO'],how='left')
Target.Billings.fillna(0,inplace=True)
Target=Target.drop(['Billed Week',  'Created Week'],axis=1)
Target['Product']=Target.groupby('ISO')['Product'].ffill()

Out[75]: 
   BilledWeek  CreateWeek ISO Product  Billings
0      201708      201704  AT   Table    1000.0
1      201708      201705  AT   Table    1000.0
2      201708      201706  AT   Table    1000.0
3      201708      201707  AT   Table       0.0
4      201708      201708  AT   Table    1000.0
5      201705      201701  DE   Table    1000.0
6      201705      201702  DE   Table    1000.0
7      201705      201703  DE   Table       0.0
8      201705      201704  DE   Table    1000.0
9      201705      201705  DE   Table    1000.0