There is an apply
method in pandas dataframe that allows to apply some sync functions like:
import numpy as np
import pandas as pd
def fun(x):
return x * 2
df = pd.DataFrame(np.arange(10), columns=['old'])
df['new'] = df['old'].apply(fun)
What is the fastest way to do similar thing if there is an async function fun2
that has to be applied:
import asyncio
import numpy as np
import pandas as pd
async def fun2(x):
return x * 2
async def main():
df = pd.DataFrame(np.arange(10), columns=['old'])
df['new'] = 0
for i in range(len(df)):
df['new'].iloc[i] = await fun2(df['old'].iloc[i])
print(df)
asyncio.run(main())