I have a dataframe.
df = pd.DataFrame({'a':[1,1,2,2,2], 'b':['x','y','u','w','v']})
It should select the values of column b
based on value passed for column a
.
Say I want to select all the values of column b
for value 2
in column a
and produce the following string
There are 3 values for value 2.
1. u
2. w
3. v
I am doing it using for
loop as below:
x = df[df['a']==2]['b'].values
result = "There are {} values for 2.\n".format(len(x))
z = ""
for i, val in enumerate(x):
temp = "{}. {}\n".format(i+1, val)
z += temp
print(result+z)
Above solution is working but I'm looking for more efficeint solution.
Context: I'm creating a chatbot response. So trying to reduce the response time
Any suggestions are welcome.