如果列中的值为非null,则在pandas中创建新的派生列

时间:2017-10-25 10:18:33

标签: python pandas

我的输入数据

SL.NO   Name
1      KING  BATA
2   
3   
4     AGS
5     FORMULA GROWTH 
6   
7     Bag

输出

SL.NO   Name               Value
1     KING  BATA          Present
2                         Not Present
3                         Not Present
4   AGS                   Present
5   FORMULA GROWTH       Present
6                        Not Present
7   Bag                  Present

如何处理pandas中的null,blank和垃圾值?

2 个答案:

答案 0 :(得分:1)

使用numpy.where

#If missing value is NaN
df['Value'] = np.where(df['Name'].isnull(), 'Present', 'Not Present')

或者:

#If missing value is empty string
df['Value'] = np.where(df['Name'].eq(''), 'Present', 'Not Present')

答案 1 :(得分:1)

有趣method(){ List<int> list1 = new List<int>{ 1, 2, 3, 4, 5, 6}; List<int> list2 = new List<int>{ 1, 2, 3 }; List<int> list3 = new List<int>{ 1, 2 }; var a = new A(); a.BList = new List<B>{ new B { Key = "b1", IntegerList = list1, new B { Key = "b2", IntegerList = list2 new B { Key = "b3", IntegerList = list3 } }

pd.Categorical

顺便提一下,无论您的缺失值是df SL.NO Name 0 1 KING BATA 1 2 2 3 3 4 AGS 4 5 FORMULA GROWTH 5 6 6 7 Bag df['Value'] = pd.Categorical.from_codes(df.Name.astype(bool), categories=['Not Present', 'Present']) df SL.NO Name Value 0 1 KING BATA Present 1 2 Not Present 2 3 Not Present 3 4 AGS Present 4 5 FORMULA GROWTH Present 5 6 Not Present 6 7 Bag Present NaN还是None,都可以使用,因为''利用了这些错误的优缺点值:

astype(bool)