[英]Creating percent of total column in pandas
I can't seem to figure out how to add a % of total column for each state and year in pandas.我似乎无法弄清楚如何在 pandas 中为每个 state 和年份添加总列的百分比。
my data looks like this我的数据看起来像这样
year type Arizona_total Utah_total California_total Colorado_total
2018 Total 163,176 90,344 343,343 32,343
2018 bio. 272 270 234. 2343
The data then continues for each major back until 1990. I want to create a column that is a percent of total majors in that state during that year for each state.然后,每个专业的数据一直持续到 1990 年。我想为每个 state 在该年创建一个占该 state 总专业百分比的列。
I am a newbie and I apologize for the simple / poorly worded question.我是新手,对于这个简单/措辞不佳的问题,我深表歉意。
Try this.尝试这个。
total = np.sum(df.ix[:,'Arizona_total':].values)
df['percent'] = df.ix[:,'Arizona_total':].sum(axis=1)/total * 100
df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.