This is part of an ongoing series of issues I'm having trying to condense a csv file with multiple rows for each client based on the number of medical services they received. For each service, they have a row. I've included the dataframe at the bottom.
I'm trying to calculate how many times a client (identified with an ID_profile number) got each type of service and add that to a column named for the type of service. So, if a client got 3 Early Intervention Services, I would add the number "3" to the "eisserv" column. Once that is done, I want to combine all the client rows into one.
Where I'm getting stuck is populating 3 different columns with data based off one column. I am trying to iterate through the rows using some strings for the function to compare to. The function works, but for reasons I can't understand, all the strings change to "25" as the function works.
import pandas as pd
df = pd.read_csv('fakeRWclient.csv')
df['PrimaryServiceCategory'] = df['PrimaryServiceCategory'].map({'Referral for Health Care/Supportive Services': '33', 'Health Education/Risk reduction': '25', 'Early Intervention Services (Parts A and B)': '11'})
df['ServiceDate'] = pd.to_datetime(df['ServiceDate'], format="%m/%d/%Y")
df['id_profile'] = df['id_profile'].apply(str)
df['served'] = df['id_profile'] + " " + df['PrimaryServiceCategory']
df['count'] = df['served'].map(df['served'].value_counts())
eis = "11"
ref = "33"
her = "25"
print("Here are the string values")
print(eis)
print(ref)
print(her)
df['herrserv']=""
df['refserv']=""
df['eisserv']=""
for index in df.itertuples():
for eis in df['PrimaryServiceCategory']:
df['eisserv'] = df['count']
for her in df['PrimaryServiceCategory']:
df['herrserv'] = df['count']
for ref in df['PrimaryServiceCategory']:
df['refserv'] = df['count']
print("Here are the string values")
print(eis)
print(ref)
print(her)
Here is the output:
Here are the string values
11
33
25
Here are the string values
25
25
25
id_profile ServiceDate PrimaryServiceCategory served count herrserv
\
0 439 2017-12-05 25 439 25 1 1
1 444654 2017-01-25 25 444654 25 2 2
2 56454 2017-12-05 33 56454 33 1 1
3 56454 2017-01-25 25 56454 25 2 2
4 444654 2017-03-01 25 444654 25 2 2
5 56454 2017-01-01 25 56454 25 2 2
6 12222 2017-01-05 11 12222 11 1 1
7 12222 2017-01-30 25 12222 25 3 3
8 12222 2017-03-01 25 12222 25 3 3
9 12222 2017-03-20 25 12222 25 3 3
refserv eisserv
0 1 1
1 2 2
2 1 1
3 2 2
4 2 2
5 2 2
6 1 1
7 3 3
8 3 3
9 3 3
Why do the string values switch? And is this even the right function to do what I'm hoping to do?
for index in df.itertuples(): for eis in df['PrimaryServiceCategory']: df['eisserv'] = df['count'] for her in df['PrimaryServiceCategory']: df['herrserv'] = df['count'] for ref in df['PrimaryServiceCategory']: df['refserv'] = df['count']