Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我有一个返回序列的函数。I';我希望将此函数作为新列映射到另一个数据帧_Python_Pandas - Fatal编程技术网

Python 我有一个返回序列的函数。I';我希望将此函数作为新列映射到另一个数据帧

Python 我有一个返回序列的函数。I';我希望将此函数作为新列映射到另一个数据帧,python,pandas,Python,Pandas,我有一个返回序列的函数。我希望将一个函数映射到一个dataframe中,使该函数不覆盖现有列,而是使用序列索引中的列名创建新列 #SITUATION: ##Existing 1 dimensional dataframe. Could be series too: df = pd.DataFrame({'Fruit' : ['Apple','Banana','Grapes','Oranges']}) ##The function I'm trying to apply to each frui

我有一个返回序列的函数。我希望将一个函数映射到一个dataframe中,使该函数不覆盖现有列,而是使用序列索引中的列名创建新列

#SITUATION:
##Existing 1 dimensional dataframe. Could be series too:
df = pd.DataFrame({'Fruit' : ['Apple','Banana','Grapes','Oranges']})

##The function I'm trying to apply to each fruit:
def my_func(fruit):
    series = get weight,taste, shape, price etc using an API (which is a pandas series)
    return series

#DESIRED OUTPUT

Fruit    Weight    Taste    Shape    Price
Apple     6        Tarty    Oval      $2
Banana    5        Sweet    Long      $1
Grapes    3        Sweet    Round     $4
Oranges   5        Acidic   Round     $2

#CURRENT OUTPUT (Only one column with whole series embedded inside the element)
FRUIT
Weight:6, Taste:Tary, Shape:Oval, Price:$2
Weight:5, Taste:Sweet, Shape:Long, Price:$1
Weight:3, Taste:Sweet, Shape:Round, Price:$4
Weight:5, Taste:Acidic,, Shape:Round, Price:$2


我尝试过使用applymap并尝试取消堆叠,但没有成功。非常感谢您的帮助

此示例将对您有所帮助

df = pd.DataFrame([[1,2,4], [3,5,6]], columns=['a','b','c'])
a = pd.Series([1,2,3], index=['a','b','c'])
b = pd.Series([2,2,3], index=['a','b','c'])
print(df)
df = df.append(a, ignore_index=True)
print(df)
df = df.append(b, ignore_index=True)
print(df)

您可以执行以下操作:

df.set_index('Fruit', drop=False, inplace=True)
df = pd.concat([my_func(fruit) for fruit in df.Fruit],
               axis='columns').T.set_index(df.Fruit)
使用数据帧
df

api = {
    'Apple': pd.Series({'Weight': 6, 'Taste': 'Tary', 'Shape': 'Oval', 'Price': '$2'}),
    'Banana': pd.Series({'Weight': 5, 'Taste': 'Sweet', 'Shape': 'Long', 'Price': '$1'}),
    'Grapes': pd.Series({'Weight': 3, 'Taste': 'Sweet', 'Shape': 'Round', 'Price': '$4'}),
    'Oranges': pd.Series({'Weight': 5, 'Taste': 'Acidic', 'Shape': 'Round', 'Price': '$2'})
}

def my_func(fruit):
    return api[fruit]
结果是:

        Weight   Taste  Shape Price
Fruit                              
Apple        6    Tary   Oval    $2
Banana       5   Sweet   Long    $1
Grapes       3   Sweet  Round    $4
Oranges      5  Acidic  Round    $2
如果出于某种原因必须使用
applymap
,则可以执行以下操作:

num_fruits = df.shape[0]
df.set_index('Fruit', drop=False, inplace=True)
df = df.applymap(my_func).explode('Fruit')
df.rename(columns={'Fruit': 'Values'}, inplace=True)
df['Columns'] = ['Weight', 'Taste', 'Shape', 'Price'] * num_fruits
df = df.pivot(columns=['Columns'], values=['Values'])
df = df.droplevel(0, axis='columns')
df.columns.name = None

谢谢你的帮助。尝试了for循环,结果成功了。