How can I filter for string values in a mixed datatype object in Python Pandas Dataframe
<p>I have a column in a Pandas Dataframe like:(whose value_counts are shown below)</p>

<pre><code>1                      246804
2                      135272
5                        8983
8                        3459
4                        3177
6                        1278
9                         522
D                         314
E                          91
0                          29
F                          20    
Name: Admission_Source_Code, dtype: int64
</code></pre>

<p>As you can see it contains both integers and letters. I'm having to write a function where I would have to filter and search for the lettered values.</p>

<p>I was initially importing this dataset using pd.read_excel, but after having read multiple bug reports, it seems that read_excel doesnt have option to explicitly read a column as a string.</p>

<p>So I tried reading using pd.read_csv which has the dtype option. Initially this column was being stored as float64 by default, now even though I have tried to run</p>

<pre><code>Df_name['Admission_Source_Code'] = Df_name['Admission_Source_Code'].astype(int).astype('str')
</code></pre>

<p>I'm unable to format it as a string column.</p>

<p>Hence, when I filter for </p>

<pre><code>Accepted[Accepted['Admission_Source_Code']==1]
</code></pre>

<p>it works, but </p>

<pre><code>Accepted[Accepted['Admission_Source_Code']=='E']
</code></pre>

<p>still returns no results. When i try and say str(column_name) in the mask, it says invalid literal.
Can someone please help me on how would i go about either changing the dtype or how to filter for lettered values?</p>

<p>Thanks.</p>

<p>P.S. even formatting as object doesnt help</p>

<p>I did some tests using your example and filters works well, for example:</p>

<pre><code>df = pandas.read_csv('Yourfile.csv')
df['Admission_Source_Code'].value_counts()

1                      246804
2                      135272
5                        8983
8                        3459
4                        3177
6                        1278
9                         522
D                         314
E                          91
0                          29
F                          20    
Name: Admission_Source_Code, dtype: int64
</code></pre>

<p>If I try:</p>

<pre><code>print (df[(df['Admission_Source_Code']==1)])
</code></pre>

<p>I got:</p>

<pre><code>Empty DataFrame
Columns: [Admission_Source_Code]
Index: []
</code></pre>

<p>However with a <code>list comprehesion</code></p>

<pre><code>df['Admission_Source_Code'] = [str(i) for i in df['Admission_Source_Code']]
</code></pre>

<p>Using a data example:</p>

<p><a href="http://i.stack.imgur.com/Pmjfc.png" rel="nofollow"><img src="http://i.stack.imgur.com/Pmjfc.png" alt="enter image description here"></a></p>

<p>If problems persist, have you considered clean items from csv columns? <em>(i.e. a whitespace)</em>.</p>

<p>For example using the same <code>list comprehesion</code> and <code>strip()</code>:</p>

<pre><code>df['Admission_Source_Code'] = [str(i.strip()) for i in df['Admission_Source_Code']]
</code></pre>

python pandas dataframe