I’m trying to retrieve one row of data from my Dataframe created from a csv file accessed via URL.
JavaScript
x
4
1
URL = 'http://www.bom.gov.au/clim_data/cdio/tables/text/IDCJCM0033_091126.csv'
2
s = requests.get(URL).content
3
df1 = pd.read_csv(io.StringIO(s.decode('utf-8')), sep=',', escapechar='\', skiprows=11)
4
I’m using…
df1['Statistic Element'].str.contains('Mean rainfall')
…to determine the row containing the data I require however python does not recognize the .str element of the dataframe?
The code works fine using a test dataframe so I’m wondering if there is a problem with the csv?
Advertisement
Answer
remove decode
and StringIO
and try again. here I have downloaded your ‘.csv’ file and renamed it “cs.csv”. nothing is wrong with it:
JavaScript
1
4
1
from pandas import
2
df = pd.read_csv('cs.csv', sep=',', escapechar='\', skiprows=11)
3
# even do not need sep and escapechar.
4
JavaScript
1
61
61
1
>>> df['Statistic Element'].str.contains('Mean rainfall')
2
3
0 False
4
1 False
5
2 False
6
3 False
7
4 False
8
5 False
9
6 False
10
7 False
11
8 False
12
9 False
13
10 False
14
11 False
15
12 False
16
13 False
17
14 False
18
15 False
19
16 False
20
17 False
21
18 False
22
19 False
23
20 False
24
21 False
25
22 False
26
23 True
27
24 False
28
25 False
29
26 False
30
27 False
31
28 False
32
29 False
33
30 False
34
31 False
35
32 False
36
33 False
37
34 False
38
35 False
39
36 False
40
37 False
41
38 False
42
39 False
43
40 False
44
41 False
45
42 False
46
43 False
47
44 False
48
45 False
49
46 False
50
47 False
51
48 False
52
49 False
53
50 False
54
51 False
55
52 False
56
53 False
57
54 False
58
55 False
59
56 False
60
Name: Statistic Element, dtype: bool
61