提问人:Prmake 提问时间:11/23/2022 更新时间:11/23/2022 访问量:46
Pandas dataframe - 根据日期时间类型删除/忽略行
Pandas dataframe - removing/ignoring rows based on datetime type
问:
我正在处理两列的数据帧:
Order Value
1001969 1,61
2403215 301,36
2440229 1902-10-19 14:38:24
2450827 1851,07
... ...
我想对“值”列进行计算,但由于逗号作为小数分隔符,这被识别为对象。但是,使用会导致错误:。这是由数据集中的一些错误引起的,其中有日期而不是数字。 df['Value'] = df['Value'].astype(float)
TypeError: float() argument must be a string or a number, not 'datetime.datetime'
因此,我要么需要删除这些包含日期时间的行,要么忽略它们。由于某种原因,这不起作用。我试过了,但结果是.df = df.apply(pd.to_numeric, errors = 'coerce').dropna()
AttributeError: 'list' object has no attribute 'apply'
似乎这两个步骤都需要先发生另一个步骤。我想需要浮点数而不是带逗号(对象)的数字,而首先转换为浮点数需要删除日期时间值。pd.to_numeric
有什么建议吗?
答: 暂无答案
评论
type(df)
df.head().to_dict()
type(df)
结果为 'Order' = float64, 'Value' = object。 结果为 {'Order': {57: 1001969.0, 400: 2403215.0, 689: 2440229.0, 742: 2450827.0}, {'Value':{57: 1.61, 400: 301.36, 689: datetime.datetime(1902, 10, 19, 14, 38, 24), 742: 1851.07}}df.head().to_dict()
to_dict
{
}
df=pd.DataFrame({'Order': {57: 1001969.0, 400: 2403215.0, 689: 2440229.0, 742: 2450827.0}, 'Value':{57: 1.61, 400: 301.36, 689: datetime.datetime(1902, 10, 19, 14, 38, 24), 742: 1851.07}}) ; df = df.apply(pd.to_numeric, errors = 'coerce').dropna()
df.head()to_dict()
df = df.apply(pd.to_numeric, errors = 'coerce').dropna()
AttributeError: 'dict' object has no attribute 'apply'