pandas 访问pandas value_counts 的第一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36973387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing first column of pandas value_counts
提问by emma_391
I'm trying to use value_counts() function from Python's pandas package to find the frequency of items in a column. This works and outputs the following:
我正在尝试使用 Python 的 pandas 包中的 value_counts() 函数来查找列中项目的频率。这有效并输出以下内容:
57 1811
62 630
71 613
53 217
59 185
68 88
52 70
Name: hospitalized, dtype: int64
In which the first column is the item and the right column is its frequency in the column.
其中第一列是项目,右列是它在列中的频率。
From there, I wanted to access the first column of items and iterate through that in a for loop. I want to be able to access the item of each row and check if it is equal to another value. If this is true, I want to be able to access the second column and divide it by another number.
从那里,我想访问第一列项目并在 for 循环中遍历它。我希望能够访问每一行的项目并检查它是否等于另一个值。如果这是真的,我希望能够访问第二列并将其除以另一个数字。
My big issue is accessing the first column from the .value_counts() output. Is it possible to access this column and if so, how? The columns aren't named anything specific (since it's just the value_counts() output) so I'm unsure how to access them.
我的大问题是从 .value_counts() 输出访问第一列。是否可以访问此列,如果可以,如何访问?这些列没有命名任何特定的东西(因为它只是 value_counts() 输出)所以我不确定如何访问它们。
回答by ayhan
value_counts
returns a Pandas Series:
value_counts
返回一个 Pandas 系列:
df = pd.DataFrame(np.random.choice(list("abc"), size=10), columns = ["X"])
df["X"].value_counts()
Out[243]:
c 4
b 3
a 3
Name: X, dtype: int64
For the array of individual values, you can use the index of the Series:
对于单个值的数组,您可以使用系列的索引:
vl_list = df["X"].value_counts().index
Index(['c', 'b', 'a'], dtype='object')
It is of type "Index" but you can iterate over it:
它是“索引”类型,但您可以对其进行迭代:
for idx in vl_list:
print(idx)
c
b
a
Or for the numpy array, you can use df["X"].value_counts().index.values
或者对于 numpy 数组,您可以使用 df["X"].value_counts().index.values
回答by Renaud
Use Panda's iteritems()
:
使用Pandas的iteritems()
:
df = pd.DataFrame({'mycolumn': [1,2,2,2,3,3,4]})
for val, cnt in df.mycolumn.value_counts().iteritems():
print 'value', val, 'was found', cnt, 'times'
value 2 was found 3 times
value 3 was found 2 times
value 4 was found 1 times
value 1 was found 1 times
回答by Mimii Cheng
You can access the first column by using .keys()
or index
as below:
您可以使用.keys()
或访问第一列,index
如下所示:
df.column_name.value_counts().keys()
df.column_name.value_counts().keys()
df.column_name.value_counts().index
df.column_name.value_counts().index