I have a dataframe where the first column is a list, how can I iterate through the list and add a value to the relevant pre defined column:
workflow cost cam gdp ott pdl
['cam', 'gdp', 'ott'] $2,346
['pdl', 'ott'] $1,200
should convert to:
workflow cost cam gdp ott pdl
['cam', 'gdp', 'ott'] $2,346 782 782 782
['pdl', 'ott'] $1,200 600 600
I can get the length of the list, but I can't work out how to iterate over the list in order to match it to a column heading. Basically the cost is simply split evenly between the number of processes in the list.
Another alternative:
df1 = (
df.assign(cost=
df["cost"].str.replace(r"\$|,", "", regex=True).astype("float")
/ df["workflow"].str.len()
)
.explode("workflow")
.pivot(columns="workflow", values="cost")
)
df = pd.concat([df[["workflow", "cost"]], df1], axis=1)
Result for the sample:
workflow cost cam gdp ott pdl
0 [cam, gdp, ott] $2,346 782.0 782.0 782.0 NaN
1 [pdl, ott] $1,200 NaN NaN 600.0 600.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With