我正在使用Python的pandas模块生成透视表报告。源数据包括许多以毫秒为单位测量的读数。如果毫秒数超过999,则CSV文件中的值将包含逗号(例如1,234 = 1.234秒)。
以下是我尝试运行报告的方式:
问题在于平均使用时间(ms)的数据类型是对象,因此
我试图通过以下方式解决问题:
以下是我尝试运行报告的方式:
import pandas as pd
import numpy as np
pool_usage = pd.read_csv("c:/foo/ds-dump.csv")
# Add a column to the end that shows you where the data came from
pool_usage["Source File"] = "ds-dump.csv"
report = pool_usage.pivot_table(values=['Average Pool Size', 'Average Usage Time (ms)'], index=['Source File'], aggfunc=np.max)
print(report)
问题在于平均使用时间(ms)的数据类型是对象,因此
np.max
函数将其视为NaN。因此,我永远不会看到任何大于999的值。我试图通过以下方式解决问题:
import pandas as pd
import numpy as np
pool_usage = pd.read_csv("c:/foo/ds-dump.csv")
# Add a column to the end that shows you where the data came from
pool_usage["Source File"] = "ds-dump.csv"
# Convert strings to numbers if possible
pool_usage = pool_usage.convert_objects(convert_numeric=True)
report = pool_usage.pivot_table(values=['Average Pool Size', 'Average Usage Time (ms)'], index=['Source File'], aggfunc=np.max)
print(report)
这实际上改变了平均使用时间列的数据类型为浮点数,但是所有大于999的值仍然被视为NaN。
我如何将平均使用时间列转换为浮点数,即使其中一些值可能包含逗号?