英文:
'float' object has no attribute 'rint'
问题
DataFrame的所有单元格都是浮点型,但仍然无法四舍五入。
DataFrame
我正在执行以下操作:
df_lmsd['LMSD']=np.around(df_lmsd['LMSD'],2)
df_lmsd['LMSD(#)']=np.around(df_lmsd['LMSD(#)'],2)
df_lmsd
Python版本:Python 3.10.9
IDE:Jupyter Notebook
英文:
All the cells of DataFrame are of float type but still it is not able to round off.
DataFrame
I am doing this:
df_lmsd['LMSD']=np.around(df_lmsd['LMSD'],2)
df_lmsd['LMSD(#)']=np.around(df_lmsd['LMSD(#)'],2)
df_lmsd
Python Version:- Python 3.10.9
Ide:- Jupyter Notebook
答案1
得分: 3
存储类型为object
的LMSD列,而不是float
。为了演示这一点,我已经重现了错误。
输入数据(将LMSD列存储为float
类型)
import pandas as pd
import numpy as np
data = {
"created_date": ["2023-03-01", "2023-03-03", "2023-03-07", "2023-03-08", "2023-03-09"],
"lmsd": [99.701135, 100.0, 4.142565, 14.182277, 24.433356],
"lmsd_hash": [99.853902, 100.0, 4.579792, 14.58302, 23.937297]
}
df = pd.DataFrame(data)
# 将日期转换为日期时间对象
df["created_date"] = pd.to_datetime(df["created_date"])
print(df)
created_date lmsd lmsd_hash
0 2023-03-01 99.701135 99.853902
1 2023-03-03 100.000000 100.000000
2 2023-03-07 4.142565 4.579792
3 2023-03-08 14.182277 14.583020
4 2023-03-09 24.433356 23.937297
df.info()
的输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 created_date 5 non-null datetime64[ns]
1 lmsd 5 non-null float64
2 lmsd_hash 5 non-null float64
dtypes: datetime64[ns](1), float64(2)
memory usage: 248.0 bytes
将LMSD列存储为object
类型
# 将LMSD列表示为对象
df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(object)
print(df)
created_date lmsd lmsd_hash
0 2023-03-01 99.701135 99.853902
1 2023-03-03 100.0 100.0
2 2023-03-07 4.142565 4.579792
3 2023-03-08 14.182277 14.58302
4 2023-03-09 24.433356 23.937297
(请注意,这与您的DataFrame匹配)
df.info()
的输出:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 created_date 5 non-null datetime64[ns]
1 lmsd 5 non-null object
2 lmsd_hash 5 non-null object
dtypes: datetime64[ns](1), object(2)
memory usage: 248.0+ bytes
尝试在object
列中四舍五入
# 尝试在LMSD列中(表示为对象)四舍五入值
df['lmsd'] = np.around(df['lmsd'], 2)
df['lmsd_hash'] = np.around(df['lmsd_hash'], 2)
会产生以下错误,与您收到的错误相匹配:
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method
在四舍五入之前将列表示为float
为了解决这个问题,确保在四舍五入之前将LMSD列存储为float
,使用astype()
方法:
# 将LMSD列转换为float值。
df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(float)
# 将值四舍五入到小数点后两位
df['lmsd'] = np.around(df['lmsd'], 2)
df['lmsd_hash'] = np.around(df['lmsd_hash'], 2)
print(df)
输出:
created_date lmsd lmsd_hash
0 2023-03-01 99.70 99.85
1 2023-03-03 100.00 100.00
2 2023-03-07 4.14 4.58
3 2023-03-08 14.18 14.58
4 2023-03-09 24.43 23.94
英文:
It appears that you have the LMSD columns stored as the type object
rather than float
. To demonstrate this, I have reproduced the error.
Input Data (with the LMSD columns stored as type float
)
import pandas as pd
import numpy as np
data = {
"created_date": ["2023-03-01", "2023-03-03", "2023-03-07", "2023-03-08", "2023-03-09"],
"lmsd": [99.701135, 100.0, 4.142565, 14.182277, 24.433356],
"lmsd_hash": [99.853902, 100.0, 4.579792, 14.58302, 23.937297]
}
df = pd.DataFrame(data)
# Convert the dates to datetime objects
df["created_date"] = pd.to_datetime(df["created_date"])
print(df)
created_date lmsd lmsd_hash
0 2023-03-01 99.701135 99.853902
1 2023-03-03 100.000000 100.000000
2 2023-03-07 4.142565 4.579792
3 2023-03-08 14.182277 14.583020
4 2023-03-09 24.433356 23.937297
Output of df.info()
:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 created_date 5 non-null datetime64[ns]
1 lmsd 5 non-null float64
2 lmsd_hash 5 non-null float64
dtypes: datetime64[ns](1), float64(2)
memory usage: 248.0 bytes
Storing the LMSD columns as type object
# Represent the LMSD columns as objects
df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(object)
print(df)
created_date lmsd lmsd_hash
0 2023-03-01 99.701135 99.853902
1 2023-03-03 100.0 100.0
2 2023-03-07 4.142565 4.579792
3 2023-03-08 14.182277 14.58302
4 2023-03-09 24.433356 23.937297
(Notice how this matches your DataFrame)
Output of df.info()
:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 created_date 5 non-null datetime64[ns]
1 lmsd 5 non-null object
2 lmsd_hash 5 non-null object
dtypes: datetime64[ns](1), object(2)
memory usage: 248.0+ bytes
Attempting to round object
columns:
# Try to round the values in the LMSD columns (represented as objects)
df['lmsd'] = np.around(df['lmsd'], 2)
df['lmsd_hash'] = np.around(df['lmsd_hash'], 2)
The following error is produced, matching the one you were given:
<pre>
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method
</pre>
Representing the columns as float
before rounding
To fix this, ensure that the LMSD columns are stored as floats before rounding, using the .astype()
method:
# Convert the LMSD columns to float values.
df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(float)
# Round the values to two decimal places
df['lmsd'] = np.around(df['lmsd'], 2)
df['lmsd_hash'] = np.around(df['lmsd_hash'], 2)
print(df)
Output:
created_date lmsd lmsd_hash
0 2023-03-01 99.70 99.85
1 2023-03-03 100.00 100.00
2 2023-03-07 4.14 4.58
3 2023-03-08 14.18 14.58
4 2023-03-09 24.43 23.94
答案2
得分: 0
你或许可以这样做:
import numpy as np
import pandas as pd
df_lmsd = pd.DataFrame({'LMSD': [1.23456789], 'LMSD(#)': [2.34567890]})
df_lmsd['LMSD'] = np.around(np.array(df_lmsd['LMSD']), 2)
df_lmsd['LMSD(#)'] = np.around(np.array(df_lmsd['LMSD(#)']), 2)
print(df_lmsd)
这将会得到:
LMSD LMSD(#)
0 1.23 2.35
英文:
You should maybe do this:
import numpy as np
import pandas as pd
df_lmsd = pd.DataFrame({'LMSD': [1.23456789], 'LMSD(#)': [2.34567890]})
df_lmsd['LMSD'] = np.around(np.array(df_lmsd['LMSD']), 2)
df_lmsd['LMSD(#)'] = np.around(np.array(df_lmsd['LMSD(#)']), 2)
print(df_lmsd)
which gives
LMSD LMSD(#)
0 1.23 2.35
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论