Can't replace string content in pandas DataFrame

Viewed 455

I have values from submission that I appended into a data frame that look like the following:

df['SalePrice'] = submission

0       [381659.34]
1       [639894.75]
2       [189159.39]
3       [203588.45]
4       [609621.25]

I tried to remove the brackets from each side of the float with this code:

df['SalePrice'] = df.SalePrice.replace("[","").replace("]","")  
print(df['SalePrice'])

I am not able to remove the brackets. Is there any way to clean-up this float in Python?

2 Answers

Try str and strip:

df.SalePrice.str.strip('[]')

Try flattening the list before you put it into the dataframe:

submission_flat = [item for sublist in submission for item in sublist]
df['SalePrice'] = submission_flat
Related