How to transform a slice of dataframe into a new data frame
<p>I'm new to python and I'm confused sometimes with some operations
I have a dataframe called <code>ro</code> and I also have filtered this dataframe using a specific column <code>PN 3D</code> for a specific value <code>921</code> and I assigned the results into a new data frame called <code>headlamp</code> by using the following code:</p>

<pre><code> headlamp = ro[ro['PN 3D']=="921"]
</code></pre>

<p>Does my headlamp is also a <strong>dataframe</strong> or is just a <strong>slice</strong>?
The reason I'm asking this is because I'm getting some strange warnings and results later on my script.</p>

<p>Such as, I create a new column called <code>word</code> and I assigned to <code>headlamp</code></p>

<pre><code> headlamp['word'] = ""
</code></pre>

<p>I got the following warning:</p>

<pre><code> A value is trying to be set on a copy of a slice from a DataFrame
</code></pre>

<p>After that I used the following script to assign the results to <code>headlamp['word']</code></p>

<pre><code> i = 0
 for row in headlamp['Comment'].astype(list):
     headlamp['word'][i] = Counter(str(row).split())
 i+=1
 print headlamp['word']
</code></pre>

<p>The same warning appeared and it has impacted on my results, because when I used the <code>headlamp.tail()</code>, The last rows of <code>headlamp['word']</code> were empty.</p>

<p>Does anyone has an idea what is the problem and how to fix?</p>

<p>Any help will be highly appreciated</p>

<p>Use <code>.loc</code></p>

<pre><code>headlamp = ro.loc[ro['PN 3D']=="921"]
</code></pre>

<hr>

<p>As for the rest and your comments... I'm very confused.  But this is my best guess</p>

<p><strong><em>setup</em></strong>  </p>

<pre><code>import pandas as pd
from string import ascii_lowercase

chars = ascii_lowercase + ' '
probs = [0.03] * 26 + [.22]

headlamp = pd.DataFrame(np.random.choice(list(chars), (10, 100), p=probs)).sum(1).to_frame('comment')
headlamp
</code></pre>

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

<pre><code>headlamp['word'] = headlamp.comment.str.split().apply(lambda x: pd.value_counts(x).to_dict())
headlamp
</code></pre>

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

python pandas dataframe slice