2

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.

Sociopath
  • 13,068
  • 19
  • 47
  • 75

2 Answers2

2

You can use to_string

x = df.loc[df.a == 2, 'b']

print('There are {} values for value 2.\n'.format(len(x)))
print(x.reset_index(drop = True).to_string())

There are 3 values for value 2.

0    u
1    w
2    v
Vaishali
  • 37,545
  • 5
  • 58
  • 86
  • This looks concise but it's actually taking more time than my solution. – Sociopath Feb 26 '19 at 06:27
  • 1
    @AkshayNevrekar, pandas methods are more readable but usually not as efficient as vanilla Python. I personally don't see anything wrong with using a loop. – Vaishali Feb 26 '19 at 06:29
1

Another sol:

m=df.loc[df['a'].eq(2),'b']
m.index=list(range(1,len(m)+1))
print('There are {} values for value 2.\n'.format(len(x)))
print(m.to_string())

There are 3 values for value 2.

1    u
2    w
3    v
anky
  • 74,114
  • 11
  • 41
  • 70