Convert Pandas series containing string to boolean

前端 未结 4 1118
慢半拍i
慢半拍i 2020-12-01 07:58

I have a DataFrame named df as

  Order Number       Status
1         1668  Undelivered
2        19771  Undelivered
3    100032108  Undelivered
4         


        
4条回答
  •  甜味超标
    2020-12-01 08:16

    An example of replace method to replace values only in the specified column C2 and get result as DataFrame type.

    import pandas as pd
    df = pd.DataFrame({'C1':['X', 'Y', 'X', 'Y'], 'C2':['Y', 'Y', 'X', 'X']})
    
      C1 C2
    0  X  Y
    1  Y  Y
    2  X  X
    3  Y  X
    
    df.replace({'C2': {'X': True, 'Y': False}})
    
      C1     C2
    0  X  False
    1  Y  False
    2  X   True
    3  Y   True
    

提交回复
热议问题