Home > Enterprise >  Adding a range as list to a dataframe and calculating a second list with a formulat to this list in
Adding a range as list to a dataframe and calculating a second list with a formulat to this list in

Time:11-21

I have been struggling with this for some time now and hope someone will take the time to help me so I can learn something.

I have a data frame and I want to add a range list ("num") to the frame, depending on the int of the column "num". In the second step, I want to apply a function to the range list here "x" to create "y". So each value in the list "x" will have a corresponding value in the list "y".

The individual steps are not a challenge. But putting the lists in a Frame are. Is this even possible?

Example dataframe:

``` # Example Dataframe
    df = pd.DataFrame(
    {"Foo": ["ba1", "ba2", "ba3"],
    "Foo2": ["ba4", "ba5", "ba6"],
    "num":[1,2,3]}
    )
```

First step: add a range("num"): For this step the moste promissing try was

``` # best try
df.groupby("foo")["num"].apply(lambda i: [range(0,i) for i in df["num"]])
```

But adding this wont give a list for each line of "num", just for the first one.

``` # First step Dataframe
df = pd.DataFrame(
    {"Foo": ["ba1", "ba2", "ba3"],
     "Foo2": ["ba4", "ba5", "ba6"],
     "num":[[1,2,3]],
      "x":[[1],[1,2],[1,2,3]]}
)
```

The second step is to apply a function to each number in the created range list: Generating a list of values is easily done with a for loop, but adding the results to a value list "y" faield.

``` # second step Dataframe
df = pd.DataFrame(
{"Foo": ["ba1", "ba2", "ba3"],
"Foo2": ["ba4", "ba5", "ba6"],
"num":[[1,2,3]],
"x":[[1],[1,2],[1,2,3]],
"y":[[2],[2,3],[2,3,4]]}
 )
```

The simplified function here is "x 1"

Thank you for your interest and help. All the best

Martin

CodePudding user response:

df = pd.DataFrame(
    {"Foo": ["ba1", "ba2", "ba3"],
     "Foo2": ["ba4", "ba5", "ba6"]})

df['num'] = df.index   1
df['x'] = df['num'].apply(lambda x: list(range(1, x   1)))
df['y'] = df['x'].apply(lambda x: [i   1 for i in x])

gives

   Foo Foo2  num          x          y
0  ba1  ba4    1        [1]        [2]
1  ba2  ba5    2     [1, 2]     [2, 3]
2  ba3  ba6    3  [1, 2, 3]  [2, 3, 4]
  • Related