In conclusion, my data is basically about level of drinking beer and alcohol dependence. In this chapter, I extracted data and dealt with the missing data. Instead of value_counts function, I used .groupby() and replace 9 or 99 with NaN in strings because the .groupby function will automatically ignore NaN data, which is required to be shown in this chapter. The code is as follows.
import pandas
import numpy
data = pandas.read_csv('nesarc_pds.csv', low_memory=False)
data['S2BQ1A2']=data['S2BQ1A2'].convert_objects(convert_numeric=True)
data['S2AQ5B']=data['S2AQ5B'].convert_objects(convert_numeric=True)
data['S2AQ5D']=data['S2AQ5D'].convert_objects(convert_numeric=True)
#1
print('counts for S2BQ1A2_alcohol dependence ever want to drink more')
ct1=data.groupby('S2BQ1A2').size()
print(ct1)
data['S2BQ1A2']=data['S2BQ1A2'].replace(9, 'NaN')
print('counts for S2BQ1A2_with 9 set to NAN')
cw1=data.groupby('S2BQ1A2').size()
print(cw1)
#2
print('counts for S2AQ5B_how often drank beer in last 12 months')
ct2=data.groupby('S2AQ5B').size()
print(ct2)
data['S2AQ5B']=data['S2AQ5B'].replace(99, 'NaN')
print('counts for S2AQ5B_with 99 set to NAN')
cw2=data.groupby('S2AQ5B').size()
print(cw2)
#3
print('counts for S2AQ5D_number of beer consumed on days in last 12 months')
ct3=data.groupby('S2AQ5D').size()
print(ct3)
data['S2AQ5D']=data['S2AQ5D'].replace(99, 'NaN')
print('counts for S2AQ5D_with 99 set to NAN')
cw3=data.groupby('S2AQ5D').size()
print(cw3)
Console:
counts for S2BQ1A2_alcohol dependence ever want to drink more
S2BQ1A2
1.0 4499
2.0 29949
9.0 379
dtype: int64
counts for S2BQ1A2_with 9 set to NAN
S2BQ1A2
1.0 4499
2.0 29949
NaN 379
dtype: int64
counts for S2AQ5B_how often drank beer in last 12 months
S2AQ5B
1.0 836
2.0 645
3.0 1535
4.0 2190
5.0 2451
6.0 2603
7.0 2127
8.0 1194
9.0 2268
10.0 2442
99.0 55
dtype: int64
counts for S2AQ5B_with 99 set to NAN
S2AQ5B
1.0 836
2.0 645
3.0 1535
4.0 2190
5.0 2451
6.0 2603
7.0 2127
8.0 1194
9.0 2268
10.0 2442
NaN 55
dtype: int64
counts for S2AQ5D_number of beer consumed on days in last 12 months
S2AQ5D
1.0 7122
2.0 4938
3.0 2564
4.0 1224
5.0 507
6.0 1128
7.0 118
8.0 205
9.0 28
10.0 108
11.0 6
12.0 231
13.0 3
14.0 6
15.0 21
16.0 1
17.0 4
18.0 18
20.0 7
24.0 23
25.0 1
30.0 3
36.0 1
42.0 1
99.0 78
dtype: int64
counts for S2AQ5D_with 99 set to NAN
S2AQ5D
1.0 7122
2.0 4938
3.0 2564
4.0 1224
5.0 507
6.0 1128
7.0 118
8.0 205
9.0 28
10.0 108
11.0 6
12.0 231
13.0 3
14.0 6
15.0 21
16.0 1
17.0 4
18.0 18
20.0 7
24.0 23
25.0 1
30.0 3
36.0 1
42.0 1
NaN 78
dtype: int64