I have a dataframe that I want to apply a function that take one value and will give two values as a result. I used .apply(get_data).transpose().values
to put the results to the dataframe. It worked when I had only two rows in the dataframe but didn t worked with more than two rows.
I got the "Too many values to unpack (expected 2)" error.
oil_df = pd.DataFrame({
"Oils":["Oil 1","Oil 2","Oil 3"],
"Price":["","",""],
"Unit":["","",""]})
def get_data(oil):
if oil == "Oil 1":
price = 20
unit = 50
if oil == "Oil 2":
price = 30
unit = 75
if oil == "Oil 3":
price = 40
unit = 100
return(price, unit)
oil_df["Price"], oil_df["Unit"] = oil_df["Oils"].apply(get_data).transpose().values
At first, I couldn t find a way to apply the function, so I divided the function to two pieces and applied them one by one, but it took so much longer as expected. I found this way with the help of this answer. axis=1, result_type= expand
is giving me "get_data() got an unexpected keyword argument axis " error, so removed that part.
I m open to any suggestions to make this work or another way to apply this function to the dataframe. Thank you!