熊猫:如何获得每一行的百分比

时间:2016-08-31 07:46:36

标签: python pandas sum percentage series

当我使用pandas value_count方法时,我会得到以下数据:

new_df['mark'].value_counts()

1   1349110
2   1606640
3   175629
4   790062
5   330978

如何获得每行的百分比?

1   1349110 31.7%
2   1606640 37.8%
3   175629  4.1%
4   790062  18.6%
5   330978  7.8%

我需要将每一行除以这些数据的总和。

3 个答案:

答案 0 :(得分:7)

np.random.seed([3,1415])
s = pd.Series(np.random.choice(list('ABCDEFGHIJ'), 1000, p=np.arange(1, 11) / 55.))

s.value_counts()

I    176
J    167
H    136
F    128
G    111
E     85
D     83
C     52
B     38
A     24
dtype: int64

百分比

s.value_counts(normalize=True)

I    0.176
J    0.167
H    0.136
F    0.128
G    0.111
E    0.085
D    0.083
C    0.052
B    0.038
A    0.024
dtype: float64

Per @ jezreal的建议

counts = s.value_counts()
percent = s.value_counts(normalize=True) \
           .mul(100).round(1).astype(str) + '%'
pd.DataFrame({'counts': counts, 'per': percent})

enter image description here

答案 1 :(得分:4)

我认为你需要:

#if output is Series, convert it to DataFrame
df = df.rename('a').to_frame()

df['per'] = (df.a * 100 / df.a.sum()).round(1).astype(str) + '%'

print (df)
         a    per
1  1349110  31.7%
2  1606640  37.8%
3   175629   4.1%
4   790062  18.6%
5   330978   7.8%

<强>计时

似乎更快的是使用sum作为两次value_counts

In [184]: %timeit (jez(s))
10 loops, best of 3: 38.9 ms per loop

In [185]: %timeit (pir(s))
10 loops, best of 3: 76 ms per loop

时间代码:

np.random.seed([3,1415])
s = pd.Series(np.random.choice(list('ABCDEFGHIJ'), 1000, p=np.arange(1, 11) / 55.))
s = pd.concat([s]*1000)#.reset_index(drop=True)

def jez(s):
    df = s.value_counts()
    df = df.rename('a').to_frame()
    df['per'] = (df.a * 100 / df.a.sum()).round(1).astype(str) + '%'
    return df

def pir(s):
    return pd.DataFrame({'a':s.value_counts(), 
                         'per':s.value_counts(normalize=True).mul(100).round(1).astype(str) + '%'})

print (jez(s))
print (pir(s))

答案 2 :(得分:0)

这是我认为建议的更具Python性的代码段

useState

这将输出:

import React, { useState, useEffect } from 'react';
import axios from 'axios';

interface Post {
  id: number;
  title: string;
}

const DataFetching = () => {
  const [posts, setPosts] = useState<Post[]>([]);

  useEffect(() => {
    axios.get('https://jsonplaceholder.typicode.com/posts')
      .then(res => {
        console.log(res)
        setPosts(res.data)
      })
      .catch(err => {
        console.log(err)
      })
  })
  return (
    <div>
      <ul>
        {posts.map(post => (<li key={post.id}>{post.title}</li>))}
      </ul>
    </div>
  );
};

export default DataFetching;

这也允许调整小数位数