2017-05-21 7 views
1

Ich habe ein Datenrahmen wie folgt aus:Python, Pandas ===> Erstellen Sie eine neue Spalte nach anderen Spalte

 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? 

Was ich will ist bekommen:

Erstellen einer neuen Spalte (mit dem Spaltennamen ist CreateC), deren Zeile gleich 1 ist, wenn die Spalte nt das Wort 'Create' hat. Etwas wie folgt aus:

 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 

, was ich getan habe, ist:

ich eine neue Säulenbasis auf Index erstellen dann finden Reihe gehören ‚Erstellen‘ dann Indexnummer dieser Reihe

finden
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) 

, aber ich habe nur:

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') 

Gibt es einen einfachen Weg dies zu tun?

Antwort

1

Sie können str.contains für boolean Maske verwenden und dann konvertieren True und False-1 und 0 von astype zu int und dann zu str von einem anderen astype (falls erforderlich) konvertieren:

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 

Eine andere Lösung mit 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

danke, zweite Lösung funktioniert für mich, ich habe es leicht geändert 'np.where (df ['nt']. Str.contains ('Erstellen', case = True, na = False, regex = True), ' 1 ',' 0 ') ' –

+0

ich akzeptiere es :) danke nochmal. –

+0

Vielen Dank, viel Glück! – jezrael

Verwandte Themen