Identify string in dataframe and replace content using Python
<p>I have a csv file and I loaded using Pandas. Firstly I decide to rename the columns. The dataframe is this:</p>

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

<p>My goal is to check if all the columns of each row contain the following characters <code>\n</code>. If so, the cells of the row containing the previously mentioned string must be modified in such a way that the only content left is what comes after <code>\n</code>. The output of such algorithm should be like this:</p>

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

<p>The code so far is this but I got stuck on finding and removing \n along with what precedes it. </p>

<pre><code>import pandas as pd
df = pd.read_csv('prova.csv', sep=',', skiprows=0, header=None,low_memory=False)
df.columns = ['A','B','C','D','E','F']
for index, row in df.iterrows():
    if '\n' in row[?]:
        # how do I remove the unwanted characters for each cell?
</code></pre>

<p>Notice: I want to investigate all the columns, not only those where <code>\n</code> appears.</p>

<pre><code>A    object
B    object
C    object
D    object
E     int64
F    object
dtype: object
</code></pre>

<p>IIUC, you can use <code>applymap</code> with <code>str.split</code> to split on <code>\n</code> char and take the last split:</p>

<pre><code>df['E'] = df['E'].astype(str)
df.applymap(lambda x: x.split('\n')[-1])
</code></pre>

<p><a href="http://i.stack.imgur.com/26rXK.png" rel="nofollow"><img src="http://i.stack.imgur.com/26rXK.png" alt="Image"></a></p>

<p>One liner:</p>

<pre><code>df.applymap(lambda x: x.split('\n')[-1] if type(x)==str else x)
</code></pre>

python pandas replace identity