I have a function f(featureList)
that takes a list of arbitrary length as input and gives another list of the same length as output
And I have a pandas dataframe that looks like
Class_ID Date Student_ID feature
1 1/1/2023 3 0.02167131
1 1/1/2023 4 0.17349148
1 1/1/2023 6 0.08438952
1 1/1/2023 8 0.04143787
1 1/1/2023 9 0.02589056
1 1/1/2023 1 0.03866752
1 1/1/2023 10 0.0461553
3 17/4/2022 5 0.2
3 17/4/2022 2 0.1
3 17/4/2022 3 0.55
3 17/4/2022 4 0.15
and I would like to apply the function f(featureList)
to the feature
column groupby
Class_ID
and generate a new column called New_feature
. And here’s my code:
df['New_feature'] = df.groupby('Class_ID', group_keys=False)['feature'].apply(f)
However it gives the following error:
KeyError: 0
The above exception was the direct cause of the following exception:
# If we have a listlike key, _check_indexing_error will raise```