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
findendf['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?
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 ') ' –
ich akzeptiere es :) danke nochmal. –
Vielen Dank, viel Glück! – jezrael