如何在引用csv文件中填写缺少的csv文件值

时间:2016-06-02 17:59:36

标签: python-2.7 csv pandas dataframe missing-data

我有一个像这样的参考文件

 var getData = (function () {

                $http.get(urlData)
                    .success(function (data) {
                        // Considering Angular UI-Grid $scope.gridOptions.data should be declared as is
                        $scope.gridOptions.data = data;
                        // $interval whilst we wait for the grid to digest the data we just gave it
                        $interval(function () {
                            $scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
                        }, 0, 1);
                    });
            }());

和丢失的文件

Id, Value1, Value2
a, a1, a2
b, b1, b2
c, c1, c2
d, d1, d2
...
n, n1, n2 

如何编写代码以根据参考文件填充缺失值' Id'

1 个答案:

答案 0 :(得分:0)

您可以使用WWDC video about ODRs执行此操作,但首先将您的加入列设置为DF&#39中的索引:

In [71]: df = df.set_index('Id').fillna(ref.set_index('Id')).reset_index()

In [72]: df
Out[72]:
  Id Value1 Value2
0  d     d1     d2
1  g    NaN     g2
2  a    a1      a2
3  c    c1      c2

数据:

In [69]: ref
Out[69]:
  Id Value1 Value2
0  a     a1     a2
1  b     b1     b2
2  c     c1     c2
3  d     d1     d2

In [70]: df
Out[70]:
  Id Value1 Value2
0  d    NaN     d2
1  g    NaN     g2
2  a    a1     NaN
3  c    c1     NaN