I have a dataframe that has this kind of format:
Level Nr. quantity ....
0 "" "" ....
2 "" "" ....
2 "" "" ....
2 "" "" ....
2 "" "" ....
2 "" "" ....
0 "" "" ....
2 "" "" ....
2 "" "" ....
2 "" "" ....
Every "0,2,2..." block is a group, the 0 means a new group has to be created.
I was able to do that with:
grouped_df = df.groupby( df.level.eq(0).cumsum())
This creates a DataFrameGroupBy and every group ends before the 0.
Now I want to do operations on single groups (i.e. count number of appearances of a specific string) and append the result in a new column in the original data frame, but i get this error:
<ipython-input-9-20282836353b>:70: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
I tried this:
for key, item in grouped_df:
mygroup = grouped_df.get_group(key)
mygroup['new column'] = ( (mygroup['nr.'] == "stringtobematched").sum() )
Can somebody help me? I'm sure there's a simple way to do this, but as a newbie with Pandas I have no idea.