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 think you should be able to filter your <code>value_counts</code> series using <code>.ix[]</code> or <code>.loc[]</code> indexers, filtering (indexing) by strings</p>

<p>Demo:</p>

<pre><code>In [27]: df
Out[27]:
                        Count
Admission_Source_Code
1                      246804
2                      135272
5                        8983
8                        3459
4                        3177
6                        1278
9                         522
D                         314
E                          91
0                          29
F                          20

In [28]: df.index.dtype
Out[28]: dtype('O')

In [29]: df.ix['2']
Out[29]:
Count    135272
Name: 2, dtype: int64

In [30]: df.ix[['2','E','5','D']]
Out[30]:
                        Count
Admission_Source_Code
2                      135272
E                          91
5                        8983
D                         314
</code></pre>

<p>List index values:</p>

<pre><code>In [36]: df.index.values
Out[36]: array(['1', '2', '5', '8', '4', '6', '9', 'D', 'E', '0', 'F'], dtype=object)
</code></pre>

python pandas dataframe