How to transform a slice of dataframe into a new data frame

Question:

I’m new to python and I’m confused sometimes with some operations
I have a dataframe called ro and I also have filtered this dataframe using a specific column PN 3D for a specific value 921 and I assigned the results into a new data frame called headlamp by using the following code:

 headlamp = ro[ro['PN 3D']=="921"]

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

Such as, I create a new column called word and I assigned to headlamp

 headlamp['word'] = ""

I got the following warning:

 A value is trying to be set on a copy of a slice from a DataFrame

After that I used the following script to assign the results to headlamp['word']

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

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

Does anyone has an idea what is the problem and how to fix?

Any help will be highly appreciated

Answers:

Use .loc

headlamp = ro.loc[ro['PN 3D']=="921"]

As for the rest and your comments… I’m very confused. But this is my best guess

setup

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

enter image description here

headlamp['word'] = headlamp.comment.str.split().apply(lambda x: pd.value_counts(x).to_dict())
headlamp

enter image description here

Answered By: piRSquared

To convert a slice into new dataframe, do this slice.copy()

Answered By: paulido
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.