Sheng Bi - 11 months ago 57

Python Question

I am quite new to Python, and have some basic questions which I could not find answer till now.

suppose I have the following dataframe named phone.

`current_cellphone | months of usage | previous_cellphone`

0 | Motorola | 11 | Motorola

1 | Huawei | 21 | Nokia

2 | Motorola | 13 | Motorola

3 | Nokia | 2 | iphone

4 | Huawei | 20 | Huawei

5 | Motorola | 15 | Motorola

6 | Sony | 9 | HTC

My initiative is to group by current_cellphone, select those which are counted more than once (Motorola and Huawei).

`phone['current_cellphone'].value_counts`

The result is

`Motorola | 3`

Huawei | 2

Nokia | 1

Thank you very much for your helps..

Answer Source

It's pandas.Series object. You can find that out using type().

```
In [157]: phone
Out[157]:
current_cellphone | months of usage |.1 previous_cellphone
0 Motorola | 11 | Motorola NaN NaN
1 Huawei | 21 | Nokia NaN NaN
2 Motorola | 13 | Motorola NaN NaN
3 Nokia | 2 | iphone NaN NaN
4 Huawei | 20 | Huawei NaN NaN
5 Motorola | 15 | Motorola NaN NaN
6 Sony | 9 | HTC NaN NaN
In [158]: vc = phone['current_cellphone'].value_counts()
In [159]: vc
Out[159]:
Motorola 3
Huawei 2
Nokia 1
Sony 1
Name: current_cellphone, dtype: int64
In [160]: type(vc)
Out[160]: pandas.core.series.Series
```

To extract the information from the series:

```
In [169]: vc.values
Out[169]: array([3, 2, 1, 1])
In [170]: vc.keys()
Out[170]: Index([u'Motorola', u'Huawei', u'Nokia', u'Sony'], dtype='object')
In [176]: vc.to_dict()
Out[176]: {'Huawei': 2, 'Motorola': 3, 'Nokia': 1, 'Sony': 1}
In [177]: vc.to_dict().keys()
Out[177]: ['Nokia', 'Huawei', 'Motorola', 'Sony']
In [178]: vc.to_dict().values()
Out[178]: [1, 2, 3, 1]
```

Converting to dataframe:

```
In [180]: pd.DataFrame(vc)
Out[180]:
current_cellphone
Motorola 3
Huawei 2
Nokia 1
Sony 1
```