Код: Выделить всё
def my_function(price_label):
for index, line in df.iterrows():
if df['Price'] 15000 and df['Price'] 38000:
return "High"
# Add a new column 'Price Status' based on values from other columns
df['Price Label'] = df['Price'].apply(my_function)
df
Код: Выделить всё
Traceback (most recent call last):
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\interactiveshell.py", line 3553, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\boni\AppData\Local\Temp\ipykernel_22476\186094663.py", line 11, in
df['Price Status'] = df['Price'].apply(my_function)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\series.py", line 4904, in apply
).apply()
^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\apply.py", line 1427, in apply
return self.apply_standard()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\apply.py", line 1507, in apply_standard
mapped = obj._map_values(
^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\base.py", line 921, in _map_values
return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\algorithms.py", line 1743, in map_array
return lib.map_infer(values, mapper, convert=convert)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "lib.pyx", line 2972, in pandas._libs.lib.map_infer
File "C:\Users\boni\AppData\Local\Temp\ipykernel_22476\186094663.py", line 3, in my_function
if df['Price']
Подробнее здесь: [url]https://stackoverflow.com/questions/78361304/adding-a-new-column-to-a-dataframe-with-a-function-and-condition[/url]