I have a DataFrame
and I want to get both group names and corresponding group counts as a list or numpy array. However when I convert the output to matrix I only get group counts I dont get the names. Like in the example below:
df = pd.DataFrame({'a':[0.5, 0.4, 5 , 0.4, 0.5, 0.6 ]})
b = df['a'].value_counts()
print(b)
output:
[0.4 2
0.5 2
0.6 1
5.0 1
Name: a, dtype: int64]
what I tried is print[b.as_matrix()]
. Output:
[array([2, 2, 1, 1])]
In this case I do not have the information of corresponding group names which also I need. Thank you.
Return a Series containing counts of unique values. The resulting object will be in descending order so that the first element is the most frequently-occurring element.
There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations.
count() should be used when you want to find the frequency of valid values present in columns with respect to specified col . . value_counts() should be used to find the frequencies of a series.
You can get unique values in column (multiple columns) from pandas DataFrame using unique() or Series. unique() functions. unique() from Series is used to get unique values from a single column and the other one is used to get from multiple columns.
Convert it to a dict
:
bd = dict(b)
print(bd)
# {0.40000000000000002: 2, 0.5: 2, 0.59999999999999998: 1, 5.0: 1}
Don't worry about the long decimals. They're just a result of floating point representation; you still get what you expect from the dict.
bd[0.4]
# 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With