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><strong><em>Solution</em></strong><br>
Use <code>str</code> accessor with <code>split</code> after <code>stack</code> to get a series.</p>

<pre><code>df.astype(str).stack().str.split('\n').str[-1].unstack()
</code></pre>

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

<hr>

<h3>Setup Reference</h3>

<pre><code>df = pd.DataFrame([
        ['bello', 'bot', 'corpo', '105', 245, 'Yes'],
        ['bello', 'par\nsot', 'testo\ncorpo', '105', 660, 'Yes\nno'],
        ['bello', 'pic\nhot', 'fallo', '195\n250', 660, 'Yes'],
        ['bello', 'hot', 'fallo\nbacca', '105', 245, 'Yes']
    ], columns=list('ABCDEF'))
</code></pre>

python pandas replace identity