Any tips GREATLY appreciated. I started off with the following dataframe named years69_19: years69_19
then, I created a bunch of different dataframes, which contain data from years69_19 separated by "Fied" name. Here's an example of how I did this (some departments had multiple labels for the same department so I used the | operator to find all of them): separating by field
then, I put the new dataframes into a list called listofdeps. I also made a list with strings that correspond with listofdeps, which is just for the sake of titling the dataframes correctly. here's the list of dataframes and string labels
finally, i iterated over listofdeps, and pivoted each dataframe. Here's my code for that:
newlistofdeps = []
for dataframes, deptname in zip(listofdeps, depstrings):
newlabel = deptname + ' Department at [REDACTED]'
dataframes[newlabel] = 1
deptable = pd.pivot_table(dataframes[['Year', 'Gender', 'Ethnicity', newlabel]], index=['Gender', 'Ethnicity'], columns = ['Year'], aggfunc=np.sum, fill_value=0)
newlistofdeps.append(deptable)
Now I have a list newlistofdeps, which has a dataframe for each department (field), and it looks like this: example of first dataframe in newlistofdeps
Stackoverflow community, I need help with the following:
I need to reorder the Ethnicity index like so: 'Asian', 'Black', 'Chicano/Mexican-American', 'Other Hispanic/Latino', 'White', 'Other', 'Interational'. I've tried so many different approaches like df.reindex and using "level", but I just haven't been able to figure out how to do this.
I need to make it so that for every dataframe in newlistofdeps, every ethnicity listed above appears, even if there were no rows with that ethnicity in that department. Here's an example of what I mean. enter image description here In this department, there weren't any Chicano/Mexican-American females or Black males. However, I still need rows for these groups, they would just all be filled with 0s. I actually have no clue how to go about this, I was thinking maybe creating a dataframe in this format with all the ethncities filled with 0s, and then left merging each dataframe with that dataframe so the missing ethnicities still have rows. any ideas?
Thank you!!!