Pandas:找到最大值的列名,我需要带有星号文本的过滤列?

时间:2017-02-17 07:02:34

标签: python pandas

我有这样的数据,

enter image description here

所以我将每个评级(星级)的百分比存储到这样的pandas中

enter image description here

现在我想获取最大值的列名,例如

1_star  2_star  3_star  4_star  5_star
0.023   0.112   0.474   0.316   0.075

## returns 3_star

我该怎么做?

我发现anwer完全符合我的情况Pandas second largest value's column name,但它看起来太复杂了。毕竟,我可以通过df.max()得到最大值,为什么获取最大值的列名是如此复杂?

1 个答案:

答案 0 :(得分:1)

对于Series的选择行,您似乎需要Series.idxmax iloc

print (df)
   1_star  2_star  3_star  4_star  5_star
0   0.023   0.112   0.474   0.316   0.075

print (df.iloc[0].idxmax())
3_star

如果所有DataFrame的需要值都使用DataFrame.idxmax - 输出为Series

print (df.idxmax(axis=1))

此外,您filter只能_star print (df.filter(like='_star').idxmax(axis=1)) lock "3.7.1" set :application, "railsapp.com" set :repo_url, "git@bitbucket.org:UserName/my_app.git" set :stages, ["staging", "production"] set :deploy_to, "/home/ubuntu/myapp" append :linked_files, "config/database.yml", "config/secrets.yml", "config/application.yml" append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "vendor/bundle", "public/system", "public/uploads" set :rails_env, :staging server ' xx.xxx.xxx.xxx', user: 'ubuntu', roles: %w(web app db) set :ssh_options, { :user => "ubuntu", :keys => [File.join(ENV["HOME"], ".ec2", "stage1.pem")], :forward_agent => true } 列的文字:

$ cap staging deploy --trace
** Invoke staging (first_time)
** Execute staging
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke bundler:map_bins (first_time)
** Invoke passenger:bundler:hook (first_time)
** Execute passenger:bundler:hook
** Execute bundler:map_bins
** Invoke deploy:set_rails_env (first_time)
** Execute deploy:set_rails_env
** Invoke deploy:set_linked_dirs (first_time)
** Execute deploy:set_linked_dirs
** Invoke deploy:set_rails_env 
** Invoke rvm:hook (first_time)
** Invoke passenger:rvm:hook (first_time)
** Invoke passenger:test_which_passenger (first_time)
** Execute passenger:test_which_passenger
cap aborted!
SocketError: getaddrinfo: Name or service not known
/usr/lib/ruby/2.3.0/socket.rb:231:in `getaddrinfo'
/usr/lib/ruby/2.3.0/socket.rb:231:in `foreach'
/usr/lib/ruby/2.3.0/socket.rb:626:in `tcp'
/var/lib/gems/2.3.0/gems/net-ssh-4.0.1/lib/net/ssh/transport/session.rb:70:in `initialize'
/var/lib/gems/2.3.0/gems/net-ssh-4.0.1/lib/net/ssh.rb:233:in `new'
/var/lib/gems/2.3.0/gems/net-ssh-4.0.1/lib/net/ssh.rb:233:in `start'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/connection_pool.rb:59:in `call'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/connection_pool.rb:59:in `with'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/netssh.rb:155:in `with_ssh'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/netssh.rb:108:in `execute_command'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/abstract.rb:141:in `block in create_command_and_execute'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/abstract.rb:141:in `tap'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/abstract.rb:141:in `create_command_and_execute'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/abstract.rb:55:in `test'
/var/lib/gems/2.3.0/gems/capistrano-passenger-0.2.0/lib/capistrano/tasks/passenger.cap:42:in `block (3 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/abstract.rb:29:in `instance_exec'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/backends/abstract.rb:29:in `run'
/var/lib/gems/2.3.0/gems/sshkit-1.11.5/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => rvm:hook => passenger:rvm:hook => passenger:test_which_passenger