我有一个看起来像的多索引系列
value
foo bar baz
1 A C 6
D 2
B D 6
F 4
2 B C 5
F 7
Run Code Online (Sandbox Code Playgroud)
我想总结一下foo和bar,得到每个foo,bar的值的总和,无论baz,我都可以实现df.groupby(level=[0, 1]).sum().这个系列看起来像:
sum_value
foo bar
1 A 8
B 10
2 B 12
Run Code Online (Sandbox Code Playgroud)
然而,我想value用新的来划分原始sum_value,以获得baz的百分比,给予foo和bar.
value
foo bar baz
1 A C 6/8=.75
D 2/8=.25
B D 6/10=.6
F 4/10=.5
2 B C 5/12=.42
F 7/12=.58
Run Code Online (Sandbox Code Playgroud)
我试过df.div(df.groupby(level=[0, 1]).sum()),但得到一个Not Implemented错误.谢谢!
如果我有一个看起来像的数据框
value otherstuff
0 4 x
0 5 x
0 2 x
1 2 x
2 3 x
2 7 x
Run Code Online (Sandbox Code Playgroud)
什么是获得新数据框的简洁方法
value otherstuff
0 5 x
1 2 x
2 7 x
Run Code Online (Sandbox Code Playgroud)
在哪里删除了具有相同索引的行,因此仅保留具有最大“值”的行?据我所知,df.drop_duplicates中没有选项可以保留最大值,只有第一个或最后一个出现。
我有一系列的地址,想要一个只有街道名称的系列.唯一的问题是一些地址没有门牌号,有些则有.
所以,如果我有一个看起来像的系列:
Idx
0 11000 SOUTH PARK
1 20314 BRAKER LANE
2 203 3RD ST
3 BIRMINGHAM PARK
4 E 12TH
Run Code Online (Sandbox Code Playgroud)
我会写什么功能来获得
Idx
0 SOUTH PARK
1 BRAKER LANE
2 3RD ST
3 BIRMINGHAM PARK
4 E 12TH
Run Code Online (Sandbox Code Playgroud)
在字符串开头的任何"单词"完全是由数字字符组成的?如您所见,我想保留"3RD STREET"开头的3.我正在思考一个正则表达式,但这超出了我的范围.谢谢!
给定一个数据框,其中包含特定事件的"名称",发生的年份及其类型,如下所示:
index name year type extracolumns
0 'a' 2014 'X' stuff
1 'a' 2014 'X' stuff
2 'a' 2014 'Y' stuff
3 'a' 2014 'Y' stuff
4 'a' 2015 'X' stuff
5 'a' 2015 'X' stuff
6 'a' 2015 'Y' stuff
7 'b' 2014 'X' stuff
8 'b' 2015 'Y' stuff
9 'c' 2014 'Z' stuff
Run Code Online (Sandbox Code Playgroud)
我想要一个由事件的'name'索引的数据帧,它按年份计算每个'type'的数量.理想情况下它看起来像:
index type_X_2014 type_Y_2014 type_Z_2014 type_X_2015 type_Y_2015 type_Z_2015
'a' 2 2 0 2 1 0
'b' 1 0 0 0 1 0
'c' 0 …Run Code Online (Sandbox Code Playgroud)