我使用了下面的代码;
def my_function(price_label):
for index, line in df.iterrows():
if df['Price'] <= 15000:
return "Low"
elif df['Price'] > 15000 and df['Price'] <= 38000:
return "Average"
elif 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 <module>
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'] <= 15000:
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\generic.py", line 1576, in __nonzero__
raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\interactiveshell.py", line 2144, in showtraceback
stb = self.InteractiveTB.structured_traceback(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\ultratb.py", line 1448, in structured_traceback
return FormattedTB.structured_traceback(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\ultratb.py", line 1339, in structured_traceback
return VerboseTB.structured_traceback(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\ultratb.py", line 1186, in structured_traceback
formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\ultratb.py", line 1076, in format_exception_as_a_whole
self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\ultratb.py", line 1168, in get_records
FrameInfo(
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\site-packages\IPython\core\ultratb.py", line 780, in __init__
ix = inspect.getsourcelines(frame)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\inspect.py", line 1264, in getsourcelines
lines, lnum = findsource(object)
^^^^^^^^^^^^^^^^^^
File "C:\Users\boni\AppData\Local\Programs\Python\Python312\Lib\inspect.py", line 1093, in findsource
raise OSError('could not get source code')
OSError: could not get source code
这可能是什么原因造成的?
你无法通过
df['price']
。以下是如何执行此操作:
def my_function(price_label):
if price_label <= 15000:
return "Low"
elif 15000 < price_label <= 38000:
return "Average"
else:
return "High"
import pandas as pd
data = {
'Price': [10000, 20000, 40000, 12000, 35000]
}
df = pd.DataFrame(data)
df['Price Label'] = df['Price'].apply(my_function)
print(df)
这给出了
Price Price Label
0 10000 Low
1 20000 Average
2 40000 High
3 12000 Low
4 35000 Average