I would like to know what I need to do in order to filter a dataframe, keeping unique values of Name
column, adding values from Value
column and adding a new column for counting appearances of each Name
what I have is this:
Name Type Value
0 apple A 1
1 banana B 3
2 apple A 2
3 pear P 4
4 apple A 6
5 carrot C 3
6 banana B 2
and I want to filter it into this:
Name Type AddedValue Occurrences
0 apple A 9 3
1 banana B 5 2
2 pear P 4 1
3 carrot C 3 1
How can I do it? I've tried conceive a .join
method with a where
condition set, but I cannot make it work and I'm sure that the problem is that I'm trying to translate pythonic thinking where there surely is a panda instruction which solves my problem with an elegant vector operation or something like that
Thanks in advance
Try groupby
method:
df.groupby(["Name","Type"]).agg(["count","sum"])
Result:
Value
count sum
Name Type
apple A 3 9
banana B 2 5
carrot C 1 3
pear P 1 4
However if You want to flatten columns/index use:
df2 = df.groupby(["Name","Type"]).agg(["count","sum"]).reset_index(drop=False)
df2.columns = [' '.join(col).strip() for col in df2.columns.values]
Output:
Name Type Value count Value sum
0 apple A 3 9
1 banana B 2 5
2 carrot C 1 3
3 pear P 1 4
Even more elegant solution thanks to @piRSquared:
df2 = df.groupby(['Name', 'Type']).Value.agg([('AddedValue', 'sum'), ('Occurences', 'count')]).reset_index(drop=False)
Output:
Name Type AddedValue Occurences
0 apple A 9 3
1 banana B 5 2
2 carrot C 3 1
3 pear P 4 1
Yes, just like ipj has answered you can try the groupby method in Pandas Groupby .
df = df.groupby(["Name","Type"]).agg(["count","sum"])
df.columns = df.columns.droplevel(0)
df = df.rename({"count": "AddedValue", "sum": "Occurrences"}, axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.