Py学习  »  Python

Python Dataframe通过一个名称来指定满意的行

Mainland • 3 年前 • 1257 次点击  

我有一个数据框,其中一列包含包裹重量,现在我必须将它们分配给符合要求的袋子。

我的代码:

df = pd.DataFrame({'parcel':[a,b,c,d,e],
                    'weight':[85,60,15,30,150]})
# I have bags that can take 100 kg parcels. Now I want to name the parcels 
# that go into a specific bag. The Bags are numbered 1,2,3,4,5. I want to use minimal bags possible. 

预期答案:

df = 
  parcel  weight  bag_num
0      a      85      1
1      b      60      2
2      c      15      1
3      d      30      2
4      e     150      NaN # This parcel is overweight, cannot be accommodated

我的回答是:

df['bag_num'] = df['weight']<100
df['bag_num'].replace(False,np.nan,inplace=True)
df=
  parcel  weight bag_num
4      e     150     NaN
0      a      85    True
1      b      60    True
3      d      30    True
2      c      15    True

我到了这个地方。我不能继续了?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132979
 
1257 次点击  
文章 [ 1 ]  |  最新文章 3 年前