Python 3.x 结果格式(括号)不可删除

Python 3.x 结果格式(括号)不可删除,python-3.x,pandas,Python 3.x,Pandas,初学者:请善待我。我想显示pandas DataFrame操作的结果,但无法去掉结果周围的括号。这是一个从数据帧中随机选取条目的小程序 import pandas as pd import random as rd ## enter choices choicelist=[] while True: entry =input('Enter an option (q to quit}') if entry =='q': break else:

初学者:请善待我。我想显示pandas DataFrame操作的结果,但无法去掉结果周围的括号。这是一个从数据帧中随机选取条目的小程序

import pandas as pd
import random as rd

## enter choices
choicelist=[]
while True:
    entry =input('Enter an option (q to quit}')
    if entry =='q':
        break
    else:
        choicelist.append(entry)

## create df with weights
df= pd.DataFrame(choicelist, columns= ['Choice'])
df['Weight']= 1/len(df)
df['CumWeight']=df['Weight'].cumsum()

## generate random number
a= rd.random()
selection = df['Choice'][(a<=df.CumWeight) & (a>df.CumWeight-df.Weight)].values
print ('Random selected choice: '+selection)
## there is still a bracket around the result...
但我想:“随机选择:a” PS:数据框没有括号:

df
Out[92]: 
  Choice  Weight  CumWeight
0      a     0.5        0.5
1      b     0.5        1.0

设置选择时,
.values
返回一个numpy数组,即使数组中只有一项。将字符串
'Random selected choice:'
添加到numpy数组时,它仍然是一个numpy数组,因此将使用括号打印。要解决这个问题,您可以只从选择中选择第一项:
print('Random selected choice:'+selection[0])
,它应该作为普通字符串打印,不带括号。

为什么我没有想到它。不过我很接近。正在尝试进行选择[]
df
Out[92]: 
  Choice  Weight  CumWeight
0      a     0.5        0.5
1      b     0.5        1.0