1

I have a method which I apply on a pandas series to produce two columns as shown below. One of the returned column contains lists of varying size. This produces VisibleDeprecationWarning. How can I avoid this?

I tried what's shown in this answer. But couldnt really adopt it to my situation here.

import pandas as pd
import numpy as np

def some_method(i):
    return i, np.random.randint(10, size=i)

df = pd.DataFrame(np.random.randint(10, size=100), columns=["a"])
df["c"], df["d"] = zip(*df.a.apply(some_method))
najeem
  • 1,841
  • 13
  • 29

1 Answers1

1

You can generate Series and assign to list of columns:

def some_method(i):
    return pd.Series([i, np.random.randint(10, size=i)])

df = pd.DataFrame(np.random.randint(10, size=100), columns=["a"])
df[["c", "d"]] = df.a.apply(some_method)
print (df)
    a  c                            d
0   2  2                       [2, 5]
1   1  1                          [2]
2   8  8     [2, 8, 2, 9, 1, 0, 9, 4]
3   9  9  [9, 0, 5, 6, 5, 0, 4, 9, 5]
4   8  8     [8, 7, 4, 6, 1, 7, 0, 0]
.. .. ..                          ...
95  7  7        [6, 4, 1, 5, 1, 5, 5]
96  0  0                           []
97  9  9  [7, 1, 0, 6, 0, 8, 0, 6, 4]
98  8  8     [2, 0, 4, 2, 8, 9, 5, 7]
99  1  1                          [6]

[100 rows x 3 columns]

Or create DataFrame from tuples generated of function if performance is important:

def some_method(i):
    return i, np.random.randint(10, size=i)

df = pd.DataFrame(np.random.randint(10, size=100), columns=["a"])
df[["c", "d"]] = pd.DataFrame(df.a.apply(some_method).tolist())
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252