Finding a specific string in a Dataframe column

Tags: , ,



I’m trying to retrieve one row of data from my Dataframe created from a csv file accessed via URL.

URL = 'http://www.bom.gov.au/clim_data/cdio/tables/text/IDCJCM0033_091126.csv'
s = requests.get(URL).content
df1 = pd.read_csv(io.StringIO(s.decode('utf-8')), sep=',', escapechar='\', skiprows=11)

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?

DF1.info()

The code works fine using a test dataframe so I’m wondering if there is a problem with the csv?

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:

from pandas import
df = pd.read_csv('cs.csv', sep=',', escapechar='\', skiprows=11)
# even do not need sep and escapechar.
>>> df['Statistic Element'].str.contains('Mean rainfall')

0     False
1     False
2     False
3     False
4     False
5     False
6     False
7     False
8     False
9     False
10    False
11    False
12    False
13    False
14    False
15    False
16    False
17    False
18    False
19    False
20    False
21    False
22    False
23     True
24    False
25    False
26    False
27    False
28    False
29    False
30    False
31    False
32    False
33    False
34    False
35    False
36    False
37    False
38    False
39    False
40    False
41    False
42    False
43    False
44    False
45    False
46    False
47    False
48    False
49    False
50    False
51    False
52    False
53    False
54    False
55    False
56    False
Name: Statistic Element, dtype: bool


Source: stackoverflow