خرید بک لینک

Vote count: 0

I want to group my data set and enrich it with a formatted representation of the aggregated information.

This is my data set:

h = ['A', 'B', 'C']
d = [["a", "x", 1], ["a", "y", 2], ["b", "y", 4]] 
rows = pd.DataFrame(d, columns=h)

   A  B  C
0  a  x  1
1  a  y  2
2  b  y  4

I create a pivot table to generate 0 for missing values:

pivot = pd.pivot_table(rows,index=["A"], values=["C"], columns=["B"],fill_value=0)

   C   
B  x  y
A      
a  1  2
b  0  4

I groupy by A to remove dimension B:

wanted = rows.groupby("A").sum()

   C
A   
a  3
b  4

I try to add a column with the string representation of the aggregate details:

wanted["D"] = pivot["C"].applymap(lambda vs: reduce(lambda a,b: str(a)+"+"+str(b), vs.values))

AttributeError: ("'int' object has no attribute 'values'", u'occurred at index x')

It seems that I don't understand applymap.

What I want to achieve is:

   C  D
A   
a  3  1+2
b  4  0+4

asked 26 secs ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 3 تير 1395 ساعت: 20:02

صفحه بندی