Search code examples
pythonconcurrencypython-asyncio

How to write the division of two columns of a dataframe using asyncio?


def divis(data):
    data['prom'] = data['total']/data['num2']
    return data
async def divis(data):
    data['prom'] = await (data['total']/data['num2'])
    return data
await divis(df2)

TypeError: unhashable type: 'Series'


Solution

  • Based on the question fastest way to apply an async function to pandas dataframe and its accepted answer, this will look like:

    import asyncio
    
    import numpy as np
    import pandas as pd
    
    async def fun2(x, y):
        return x / y
    
    async def divis(data):
        data['prom'] = await asyncio.gather(*(fun2(x, y) for x, y in zip(data['total'], data['num2'])))
        return data
     
    await divis(df2)