2017-05-21 94 views
1

我有一个数据帧是这样的:Python中,熊猫===>创建新列根据另一列

 nt 
12062 Python Pandas: Create new column out of other columns where value is not null 
12063 Python Pandas Create New Column with Groupby().Sum() 
12064 
12065 Python - Pandas - create “first fail” column from other column data 
12066 
12067 
12068 Creating new column in pandas based on value of other column 
12070 Merge with pandas creating new columns? 

我想要得到的是:

创建一个新的如果nt列具有“创建”字,则列(列名称为CreateC)的行等于1。事情是这样的:

 nt                    CreateC 
12062 Python Pandas: Create new column out of other columns where value is not null 1 
12063 Python Pandas Create New Column with Groupby().Sum()       1 
12064                   0 
12065 Python - Pandas - create “first fail” column from other column data  1 
12066                   0 
12067                 0 
12068 Creating new column in pandas based on value of other column 0 
12070 Merge with pandas creating new columns?       0 

什么,我所做的是:

我创建索引的新柱基 然后找到行包括“创建” 然后找到这些行的索引号

df['index1'] = df.index 
dfCreate = df[df['dataframe'].str.contains("Create", na = False)] 
dfCreateIndex = dfCreate.index.tolist() 

def CreateCs (row): 
    RowIndex1 = pd.to_numeric(row['index1'], errors='coerce') 
    for i in dfCreateIndex: 
     y = dfCreateIndex 
     if RowIndex1 == y: 
      return '1' 
     else: 
      return '0' 
df['CreateC'] = df.apply(lambda row: CreateCs(row), axis=1) 

,但我只得到了:

ValueError: ('The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0') 

有没有简单的方法来做到这一点?

回答

1

可以使用str.contains布尔面膜,然后用astype转换TrueFalse10int,然后由另一astype转换为str(如有必要):

df['CreateC'] = df['nt'].str.contains('Create', case=False).astype(int).astype(str) 
print (df) 
                 nt CreateC 
12062 Python Pandas: Create new column out of other ...  1 
12063 Python Pandas Create New Column with Groupby()...  1 
12064               0 
12065 Python - Pandas - create “first fail” column f...  1 
12066               0 
12067               0 
12068 Creating new column in pandas based on value o...  0 
12070   Merge with pandas creating new columns?  0 

与另一种解决方案numpy.where

df['CreateC'] = np.where(df['nt'].str.contains('Create', case=False), '1', '0') 
print (df) 
                 nt CreateC 
12062 Python Pandas: Create new column out of other ...  1 
12063 Python Pandas Create New Column with Groupby()...  1 
12064               0 
12065 Python - Pandas - create “first fail” column f...  1 
12066               0 
12067               0 
12068 Creating new column in pandas based on value o...  0 
12070   Merge with pandas creating new columns?  0 
+0

谢谢你, 第二种解决方案适用于我,我稍微改变了它 'np.where(df ['nt']。str.contains('Create',case = True,na = False,regex = True),' 1','0')' –

+0

我接受它:)再次感谢。 –

+0

谢谢,祝你好运! – jezrael

相关问题