I have a data frame
df = pd.DataFrame([["X",62,5],["Y",16,3],["Z",27,4]],columns=["id","total","days"])
id total days
X 62 5
Y 16 3
Z 27 4
Divide total column by days column and Create a new column plan which is a list in which No. of elements=Divisor, and the value of elements=Quotient, if any reminder is there increase those many values from negative indexing.
Expected Output:
df_out = pd.DataFrame([["X",62,5,[12,12,12,13,13]],["Y",16,3,[5, 5, 6]],["Z",27,4,[6, 7, 7, 7]]],columns=["id","total","days","plan"])
id total days plan
X 62 5 [12, 12, 12, 13, 13]
Y 16 3 [5, 5, 6]
Z 27 4 [6, 7, 7, 7]
How to do it in pandas?
CodePudding user response:
You can use a custom function:
def split(t, d):
# get floor division and remainder
x, r = divmod(t, d)
# assign divider or divider 1
# depending on the number of remainders
return [x]*(d-r) [x 1]*r
df['plan'] = [split(t, d) for t, d in zip(df['total'], df['days'])]
Output:
id total days plan
0 X 62 5 [12, 12, 12, 13, 13]
1 Y 16 3 [5, 5, 6]
2 Z 27 4 [6, 7, 7, 7]
CodePudding user response:
Mozway already provided a better solution.Yet this could be another approach with the use of costume function well with lambda.
def create_plan(plan, days, remainder):
return [plan]*days if remainder == 0 else [plan]*(days-remainder) [plan 1]*remainder
df = pd.DataFrame([["X",62,5],["Y",16,3],["Z",27,4]],columns=["id","total","days"])
# Create plan column
df["plan"] = df["total"] // df["days"]
# Create column for remainder
df["remainder"] = df["total"] % df["days"]
# Apply function to create final plan
df["plan"] = df.apply(lambda x: create_plan(x["plan"], x["days"], x["remainder"]), axis=1)
# Drop remainder column
df.drop("remainder", axis=1, inplace=True)
print(df)
Output:
id total days plan
0 X 62 5 [12, 12, 12, 13, 13]
1 Y 16 3 [5, 5, 6]
2 Z 27 4 [6, 7, 7, 7]