I apply
auf meine Datenrahmen my_df
wie unten bin mit:Python: Pandas gelten Funktion: InvalidIndexError
my_df['column_C'] = my_df.apply(lambda x : 'hello' if x['column_B'] is None else x['column_B'])
Ich möchte:
if x['column_B'] = None -> return 'hello'
if x['column_B'] != None -> return x['column_B']
Dann habe ich die folgenden Fehler:
<ipython-input-31-aa087c9a635e> in <lambda>(x)
----> 1 my_df['column_C'] = my_df.apply(lambda x : 'hello' if x['column_B'] is None else x['column_B'])
/usr/local/lib/python3.4/dist-packages/pandas/core/series.py in __getitem__(self, key)
599 key = com._apply_if_callable(key, self)
600 try:
--> 601 result = self.index.get_value(self, key)
602
603 if not is_scalar(result):
/usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_value(self, series, key)
2187 # python 3
2188 if is_scalar(key): # pragma: no cover
-> 2189 raise IndexError(key)
2190 raise InvalidIndexError(key)
2191
IndexError: ('column_B', 'occurred at index column_A')
Weiß jemand, was ich hier falsch gemacht habe?
Warum nicht: 'my_df ['column_C'] = my_df ['column_C']. Fillna ('hallo')'? – MaxU
Dies ist sicherlich ein klügerer Ansatz, danke! Immer noch neugierig, was in der Anwendungsfunktion falsch war, obwohl ... – Edamame