python - 获取 Pandas 中不包括标题的数据框行

标签 python python-3.x pandas

我有一个 CSV 文件,其屏幕截图如下所示: enter image description here 我的目标是获取不包括标题部分的数据框行。

我的目的:我使用函数 astype() 将行中的数据转换为 "float64",然后使用 pandas 将数据四舍五入到小数点后两位>round() 函数。其代码如下所示:

df = pd.read_csv('C:/Users/viral/PycharmProjects/Samples/036_20191009T132130.CSV',skiprows = 1)
df = df.astype('float64', errors="ignore")
df = df.round(decimals=2)

如您所见,我跳过了第一行以排除标题部分。

但不幸的是,数据没有四舍五入到小数点后两位。结果如下所示 : enter image description here

我不确定,但我猜 Empty Data frame 这行在对数据进行四舍五入时造成了问题。

带 header =无 enter image description here

即使 header= 1 也会以与 skiprow=1 相同的方式运行。

欢迎提出任何建议...

谢谢

最佳答案

我相信你需要:

file = 'C:/Users/viral/PycharmProjects/Samples/036_20191009T132130.CSV'

#for default columns 0,1,2, N with omit first row in original data
df = pd.read_csv(file,skiprows = 1, header=None)
#for columns names by first row in file omit skiprows and header parameters
#df = pd.read_csv(file)
#if necessary, convert to floats
df = df.astype('float64', errors="ignore")
#select only numeric columns
cols = df.select_dtypes(np.number).columns
#round only numeric cols
df[cols] = df[cols].round(decimals=2)

关于python - 获取 Pandas 中不包括标题的数据框行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58723781/

相关文章:

python - 如何在终端中使用颜色 (Python) : vscode

python - 重新采样 MultiIndexed Pandas DataFrame 并将不同的函数应用于列

python - 使用 csv.reader() 加载和访问数据时获取 AttributeError

python - 串联运算符 + 或 ,

python - 如何在 python 中修复这个快速排序分区? (处理 Numpy 数组)

python-3.x - 在 python 中使用 lstm 预测值时,“numpy.ndarray”对象没有属性 'iterrows'

python - 使用python提取部分数组元素

c++ - OpenCV Python 通过图像数据绑定(bind)非常慢的迭代

python - AttributeError : 'DataFrame' object has no attribute 'str'

python - 在 scipy 中整合多维积分