The assignment happens appropriately for each category, but if the blocks get full and there are IDs pending in the category and we assign them to a new block, the old assignments get rewritten. Also how do I print the whole new dataframe at the end, because when I am trying it only shows the D block assignment...
import pandas as pd
data = pd.read_csv("database.csv")
blocks = {"A": 5, "B": 30, "C": 50, "D": 100}
for key, value in blocks.items():
print("For block " key " capacity is " str(value))
while True:
s = input("Which category to be allocated? ")
dataFrame = data[data['Category'].str.contains(s)]
if not dataFrame.empty:
dataFrame['Block'] = key
# for loop for Sr. Number
spc = blocks[key]
i = 1
for index, row in dataFrame.iterrows():
if i > spc:
break
dataFrame.loc[index, 'Sr.'] = str(i)
i = 1
print(dataFrame)
break
print(dataFrame)
This is the input
Name,ID,Category
ABC,2020,G
WER,2021,M
XCV,2022,T
GFV,2034,M
WEQ,2021,M
WEW,2021,M
WET,2021,M
WEY,2021,M
WLK,2021,M
WYH,2021,M
expected output
Name,ID,Category,Block, Sr.
1 WER 2021 M A 1
3 GFV 2034 M A 2
4 WEQ 2021 M A 3
5 WEW 2021 M A 4
6 WET 2021 M A 5
7 WEY 2021 M B 1
8 WLK 2021 M B 2
9 WYH 2021 M B 3
CodePudding user response:
IIUC, use:
s = pd.Series(blocks)
m = df['Category'].eq('M')
(df
.loc[m]
.assign(**{'block': s.index.repeat(s)[:m.sum()],
'Sr.': lambda d: d.groupby('block')
.cumcount().add(1)
})
)
output:
Name ID Category block Sr.
1 WER 2021 M A 1
3 GFV 2034 M A 2
4 WEQ 2021 M A 3
5 WEW 2021 M A 4
6 WET 2021 M A 5
7 WEY 2021 M B 1
8 WLK 2021 M B 2
9 WYH 2021 M B 3