2

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 2

4

Try str and strip:

df.SalePrice.str.strip('[]')
Sign up to request clarification or add additional context in comments.

7 Comments

When I ran df.SalePrice.str.strip('[]'), the whole content is erased and I get NaN as an answer
That is odd. I tried with the data you provided and it worked. can you try if df.SalePrice.str.replace('[','') works?
This is my code block: submission = []; for ele in regressor.predict(input_fn=lambda: input_fn(df_test, shuffle=False, batch_size=32)): submission.append(np.expm1(ele['predictions'])) df = pd.DataFrame(columns = ['Id', 'SalePrice']); df['Id'] = df_test['Id']; df['SalePrice'] = submission; df['SalePrice'] = df.SalePrice.str.replace('[',''); print(df['SalePrice']); I get NaN as an answer
can you try type(df['SalePrice'][0])
here is the result: <class 'numpy.ndarray'>
|
2

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

1 Comment

That worked: submission_flat = [item for sublist in submission for item in sublist] df['SalePrice'] = submission_flat df['SalePrice'] = round(df['SalePrice'],2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.