The error is pretty clear. you are trying to replace characters in a float. x probably is a float. you might want to do str(x) which gets you
Copyf["text"] = [str(x).replace(':',' ') for x in df["text"]]
but i can't see a situation where a float contains a :
Answer from jonathan on Stack OverflowThe error is pretty clear. you are trying to replace characters in a float. x probably is a float. you might want to do str(x) which gets you
Copyf["text"] = [str(x).replace(':',' ') for x in df["text"]]
but i can't see a situation where a float contains a :
In my opinion problem is missing value in column, so use pandas methods Series.str.replace or Series.replace instead list comprehension:
Copydf["text"] = df["text"].str.replace(':',' ')
Or:
Copydf["text"] = df["text"].str.replace(':',' ', regex=True)
Solution with list comprehension is possible, only need if-else statement for test strings:
Copydf["text"] = [x.replace(':',' ') if isinstance(x, str) else x for x in df["text"]]
Sample:
Copydf = pd.DataFrame({'text':['qq:ww','e:', np.nan]})
df["text"] = df["text"].str.replace(':',' ')
print (df)
text
0 qq ww
1 e
2 NaN
In pandas the object type is used when there is not a clear distinction between the types stored in the column.
So, I guess that in your column, some objects are float type and some objects are str type. Or maybe, you are also dealing with NaN objects, NaN objects are float objects.
a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str type or just make the type conversion of the column you are dealing with.
b) After that, you can apply the string changes and/or deal with the NaN objects.
c) Finally, you transform your column into float type`.
Maybe it's a very rudimentary method but I would just do
listt = []
for i in data['column_name']:
listt.append(float(i))
data['FloatData'] = listt
python - AttributeError 'float' object has no attribute 'replace' - Stack Overflow
AttributeError: 'numpy.float64' object has no attribute 'replace'
AttributeError: 'float' object has no attribute 'replace'
python - What does float' object has no attribute 'replace' when I try locale.atof in Pandas? - Stack Overflow
Videos
Well, I don't know how "smart" this is, but I "fixed" it like this, at least for the time being:
df.idh = df.idh.astype(str).apply(locale.atof)
Please, do let me know the smart answer to this.
The problem has to do with null values. Replace cannot work on numpy nulls. Your solution doesn't return an error because you convert np.NaN (nulls) into 'nan' and replace can work on the string 'nan'. The problem is though that you now have 'nan' in your column instead of np.NaN. So if you run:
df[df.idh.isnull()]
It would return 0 rows even though you do have nulls in your data. The following code keeps the np.NaN while running a replace statement on that column.
def replace_percent(x):
try:
return x.replace('%', '')
except AttributeError:
return np.NaN
df_not.secularism = df_not.secularism.map(replace_percent)
Hallo Everyone,
I hope you are all fine, I have this code which I am getting error from , I run the same code on my Spyder and I do not get any error but when I run it on Jupyter Notebook I get this error :
'numpy.float64' object has no attribute 'replace'
this is the code that I have :
oli_row2 = df_oli.iloc[2].tolist()
idx = oli_row2.index("DB")
check_words = oli_row2[idx:]
exact_order = ['NN','ch','ab','An','tr','bcc',]
for i in range(len(check_words)):
if not(check_words[i].replace(" ","")==exact_order[i].replace(" ","")):
new_row = table.add_row()
new_row.cells[0].text = 'Oli'
new_row.cells[1].text = 'Words are out of order in Column'
new_row.cells[2].text = str(check_words[idx])
oli_flag = True
print('Out of order')
I googled lit bit but I could not find any solution.
Thank you in advance,
Best Regards,
Try this instead,
print(
"{:.3f}% {} ({} sentences)".format(pcent, gender, nsents)
)
Refer the latest docs for more examples and check the Py version!
You could also use {:.3%} instead of {:.3f}%.
It will transform the value into percentages automatically.
That means "{:.3%}".format(0.3) will print "30%" while you have to write "{:.3f}%".format(0.3 * 100) to get "30%" as well.