use*_*963 3 python dataframe pandas
作为 pandas 的新手,我希望从特定列中获取值的计数以及单个帧中的百分比计数。我可以得到其中之一,但不知道如何将它们添加或合并到一个框架中。想法?
框架/表格应该是这样的:
some_value, count, count(as %)
Run Code Online (Sandbox Code Playgroud)
这是我所拥有的...
import numpy as np
import pandas as pd
np.random.seed(1)
values = np.random.randint(30, 35, 20)
df1 = pd.DataFrame(values, columns=['some_value'])
df1.sort_values(by=['some_value'], inplace = True)
df2 = df1.value_counts()
df3 = df1.value_counts(normalize=True)
print(df2)
print("------")
print(df3)
Run Code Online (Sandbox Code Playgroud)
只需使用
pd.DataFrame({"count":df2,"%":df3*100})
Run Code Online (Sandbox Code Playgroud)
将一系列放入一个 df 中。
输出:
count %
some_value
34 7 35.0
32 4 20.0
33 3 15.0
31 3 15.0
30 3 15.0
Run Code Online (Sandbox Code Playgroud)