I'm sure this is quite simple, but my brain is frozen and there are so many different pivot
and transpose
methods. A hint would be nice at this stage.
I have this dataframe:
I want this:
I know how to get to here, if that helped, but I'm not sure if it does
FYI - The actual data has more columns and I need to separate out these four based on the "site" column, reformat everything, calculate some percentages, put the pieces back together, and eventually end up with something like this:
I'm hoping that if I can get on the right track for reformatting part of the data, I can repeat the process...
(then I need to figure out how to run a Chi-square test, but that's for later... :-(
The easiest resolution is df.stack
:
df = pd.DataFrame({'MIC-m': [138, 3, 22, 45],
'MIC-t': [34, 90, 30, 53],
'MIC-q': [73, 13, 53, 68],
'Total': [229, 229, 229, 229]}, index=['H', 'L', 'M', 'X'])
# Drop total, because we need sum of columns, not rows
df.drop(columns='Total', inplace=True)
# Get final result
df = pd.DataFrame(df.append(df.sum().rename('Total')).T.stack(), columns=['count'])
yields:
count
MIC-m H 138
L 3
M 22
X 45
Total 208
MIC-t H 34
L 90
M 30
X 53
Total 207
MIC-q H 73
L 13
M 53
X 68
Total 207
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.