Can pandas dataframe have dtype of list?
<p>I'm new to Pandas, I process a dataset, where one of the columns is string with pipe (<code>|</code>) separated values. Now I have a task to remove any text in this |-separated field that's not fulfilling certain criteria.</p>

<p>My naive approach is to iterate the dataframe row by row and explode the field into list and validate this way. Then write the modified row back to the original dataframe. See this metasample:</p>

<pre><code>for index, row in dataframe.iterrows():
    fixed = [x[:29] for x in row['field'].split('|')]
    dataframe.loc[index, 'field'] = "|".join(fixed)
</code></pre>

<p>Is there a better, and more importantly faster way to do this?</p>

<p>IIUC you can use:</p>

<pre><code>dataframe = pd.DataFrame({'field':['aasd|bbuu|cccc|ddde|e','ffff|gggg|hhhh|i|j','cccc|u|k'],
                          'G':[4,5,6]})

print (dataframe)
   G                  field
0  4  aasd|bbuu|cccc|ddde|e
1  5     ffff|gggg|hhhh|i|j
2  6               cccc|u|k


print (dataframe.field.str.split('|', expand=True)
                          .stack()
                          .str[:2] #change to 29
                          .groupby(level=0)
                          .apply('|'.join))

0    aa|bb|cc|dd|e
1     ff|gg|hh|i|j
2           cc|u|k
dtype: object
</code></pre>

<p>Another solution via list comprehension:</p>

<pre><code>dataframe['new'] = pd.Series([[x[:2] for x in y] for y in dataframe.field.str.split('|')], 
                             index=dataframe.index)
                     .apply('|'.join)
print (dataframe)
   G                  field            new
0  4  aasd|bbuu|cccc|ddde|e  aa|bb|cc|dd|e
1  5     ffff|gggg|hhhh|i|j   ff|gg|hh|i|j
2  6               cccc|u|k         cc|u|k
</code></pre>

<hr>

<pre><code>dataframe = pd.DataFrame({'field':['aasd|bbuu|cc|ddde|e','ffff|gggg|hhhh|i|j','cccc|u|k'],
                   'G':[4,5,6]})

print (dataframe)
   G                field
0  4  aasd|bbuu|cc|ddde|e
1  5   ffff|gggg|hhhh|i|j
2  6             cccc|u|k
</code></pre>

<p>If need filter all values with values longer as <code>2</code>:</p>

<pre><code>s = dataframe.field.str.split('|', expand=True).stack()
print (s)
0  0    aasd
   1    bbuu
   2      cc
   3    ddde
   4       e
1  0    ffff
   1    gggg
   2    hhhh
   3       i
   4       j
2  0    cccc
   1       u
   2       k
dtype: object

dataframe['new'] = s[s.str.len() &lt; 3].groupby(level=0).apply('|'.join)
print (dataframe)

   G                field   new
0  4  aasd|bbuu|cc|ddde|e  cc|e
1  5   ffff|gggg|hhhh|i|j   i|j
2  6             cccc|u|k   u|k
</code></pre>

<p>Another solution:</p>

<pre><code>dataframe['new'] = pd.Series([[x for x in y if len(x) &lt; 3] for y in dataframe.field.str.split('|')], 
                              index=dataframe.index)
                     .apply('|'.join)
print (dataframe)
   G                field   new
0  4  aasd|bbuu|cc|ddde|e  cc|e
1  5   ffff|gggg|hhhh|i|j   i|j
2  6             cccc|u|k   u|k
</code></pre>

python string list pandas list-comprehension