Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

arrays - How to remove single quote in python list

I would like to remove the single quotes from a list

Creating loop:

results = []
for k in range(1,number_of_observation+2):
results += ['X'+str(k)]

results

Output :

['X1','X2','X3','X4','X5','X6','X7','X8','X9','X10','X11','X12','X13','X14','X15','X16','X17','X18']

Actually each element in the list contains numpy array like this :

X1 = array([ 5.29869582e+03,  4.78138124e+03,  4.66993519e+03,  4.63760715e+03,
             4.24625776e+03,  6.82121026e-13,  3.67310328e+03,  3.62922983e+03,
             4.67551867e+03, -2.01596513e+03,  5.17998388e+03,  0.00000000e+00,
             5.44605355e+03,  4.51697631e+03,  4.62300856e+03,  4.44902873e+03])

X2 = array([ 5.15984732e+03,  3.69964719e+03,  4.88607026e+03,  5.06762424e+03,
             4.54623661e+03,  9.09494702e-13,  4.04998815e+03,  3.91555776e+03,
             5.07698709e+03, -1.11066480e+03,  4.49209767e+03,  4.54747351e-13,
             4.97724688e+03,  4.24955479e+03,  4.72048717e+03,  5.58904656e+03])

And i want to create dataframe from it

Final = pd.DataFrame(data = [results], columns= column_name)
Final

Desire output :

Correct Dataframe

But it gave me output like this :

Incorrect Dataframe

question from:https://stackoverflow.com/questions/65866984/how-to-remove-single-quote-in-python-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use eval function on the result list.Instead of just [results] use list comprehension as in the image. enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...