Reputation: 328
I have this data frame
df = pd.DataFrame(data = {"Store":["a","b","c","c","d"],"Goods":["x","x","x","y","x"]})
Store Goods
0 a x
1 b x
2 c x
3 c y
4 d x
I want a descriptive output that tells me each good is available in which stores, like this
The Good x is available at:
a
b
c
d
The Good y is available at:
c
so I tried this
for index, row in df.groupby(['Goods', 'Store']).count().iterrows():
print("The Good " + index[0] + " is available at:" + "\n" + index[1] + "\n" + "\n")
and got this
The Good x is available at:
a
The Good x is available at:
b
The Good x is available at:
c
The Good x is available at:
d
The Good y is available at:
c
How can I do it right?
Upvotes: 0
Views: 34
Reputation: 323316
You can just try groupby
for x , y in df.groupby('Goods'):
print('The Good {} is avaliable at:'.format(x))
print (y['Store'].to_string(index=False))
The Good x is avaliable at:
a
b
c
d
The Good y is avaliable at:
c
Upvotes: 1