Too many values to unpack – Pandas DataFrame

huangapple go评论64阅读模式
英文:

Too many values to unpack - Pandas DataFrame

问题

I have a dataframe that I want to apply a function that takes one value and will give two values as a result. I used .apply(get_data).transpose().values to put the results into the dataframe. It worked when I had only two rows in the dataframe but didn't work 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 into two pieces and applied them one by one, but it took much longer than 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 I 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!

英文:

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!

答案1

得分: 0

尝试使用 locnp.select

for column, values in [('Price', [20, 30, 40]), ('Unit', [50, 75, 100])]:
    # loc
    # oil_df.loc[oil_df['Oils'].isin(['Oil 1', 'Oil 2', 'Oil 3']), column] = values
    # np.select
    oil_df[column] = np.select(condlist=[oil_df['Oils'] == f'Oil {i}' for i in range(1, 4)], choicelist=values)

输出

    Oils Price Unit
0  Oil 1    20   50
1  Oil 2    30   75
2  Oil 3    40  100
英文:

Try loc or np.select

for column, values in [('Price', [20, 30, 40]), ('Unit', [50, 75, 100])]:
    # loc
    # oil_df.loc[oil_df['Oils'].isin(['Oil 1', 'Oil 2', 'Oil 3']), column] = values
    # np.select
    oil_df[column] = np.select(condlist=[oil_df['Oils'] == f'Oil {i}' for i in range(1, 4)], choicelist=values)

output

    Oils Price Unit
0  Oil 1    20   50
1  Oil 2    30   75
2  Oil 3    40  100

答案2

得分: 0

Here's the translated code:

不确定您真正想要实现什么但您正在对数据进行转置因此返回的数组有3个元组用于oil12和3),所以有太多值要解包

如果您的意图是填充第二和第三列那么您需要返回一个具有2列的数据框即每行一个系列),然后将其用作数据框的2列的输入

```python
import pandas as pd

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 pd.Series([price, unit])

oil_df[['Price', 'Unit']] = oil_df["Oils"].apply(get_data)

oil_df

返回

	Oils	Price	Unit
0	Oil 1	20	50
1	Oil 2	30	75
2	Oil 3	40	100

在这种情况下

oil_df["Oils"].apply(get_data)

返回

	0	1
0	20	50
1	30	75
2	40	100

并将其分配给 oil_df[['Price', 'Unit']] 合并了数据


<details>
<summary>英文:</summary>

Not sure what you are really trying to achieve with this, but you are transposing the data, so the array you return has 3 tuples (for oil1, 2, and 3) so it has too many values to unpack.

If your intent is to fill in the second and third column, then you want to return a dataframe with 2 columns (i.e, a Series per row), and then use it as input to the dataframe&#39;s 2 columns

import pandas as pd

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 pd.Series([price, unit])

oil_df[['Price', 'Unit']] = oil_df["Oils"].apply(get_data)

oil_df


returns
Oils	Price	Unit

0 Oil 1 20 50
1 Oil 2 30 75
2 Oil 3 40 100


in this case
```python
oil_df[&quot;Oils&quot;].apply(get_data)

returns

	0	1
0	20	50
1	30	75
2	40	100

and assigning this to oil_df[[&#39;Price&#39;, &#39;Unit&#39;]] merges the data

huangapple
  • 本文由 发表于 2023年6月8日 08:12:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定