如何根据python中的缺失值对列进行分区。
我在数据框中有以下表格:
function ($http, $q, ApiService, AuthService) {
return {
addNewDevice: function (deviceDtls) {
var deferred = $q.defer();
var payload = new FormData();
payload.append('deviceDtls', new Blob([JSON
.stringify(deviceDtls)], {
type: "application/json"
}));
// payload.append('profilePic', profile_pic);
payload.append('doctorId', AuthService.getDoctorId());
var req = {
method: 'POST',
headers: {
'Content-Type': undefined
},
transformRequest: angular.identity,
responseType: 'arraybuffer',
data: payload
}
ApiService.generic_post('/device/', req).then(
function (res) {
deferred.resolve(res);
}, function (error) {
deferred.reject(error);
}
);
return deferred.promise;
}
return DeviceService1;
根据NA或任何其他方式,我将如何重现下表:
Store Bag
Alberts ClothBag
Vons KateSpade
Ralphs GroceryBag1
Na apple
Na pear
Na staples
Kmart ShoppingList
Na beachball
Na milk
QuikEmart List5
Na Duff
Na Donuts
如果商店下面没有Na,那么item列将采用Bag的名称,如前两列所示。
答案 0 :(得分:0)
def f(d):
if len(d) > 1:
r = d.iloc[1:]
r['Item'] = r['Bag']
r['Bag'] = d.Bag.iat[0]
r['Store'].iat[0] = d.Store.iat[0]
return r
else:
return d.assign(Item=d.Bag)
df.groupby(df.Store.ne('Na').cumsum(), group_keys=False).apply(f)
Store Bag Item
0 Alberts ClothBag ClothBag
1 Vons KateSpade KateSpade
3 Ralphs GroceryBag1 apple
4 Na GroceryBag1 pear
5 Na GroceryBag1 staples
7 Kmart ShoppingList beachball
8 Na ShoppingList milk
10 QuikEmart List5 Duff
11 Na List5 Donuts