“Thousands”和“skip_blank_lines”参数将无法正常工作。为什么?

问题描述 投票:0回答:1

这是我的代码:

in[0]
      import pandas as pd
      df = pd.read_csv('datefile6.csv',thousands=',', skip_blank_lines=True)
      df

out[1]      month   day     year    salary   age
       0    8.0     15.0    2012.0  1400.0   25.0
       1    NaN     NaN     NaN     NaN      NaN
       2    9.0     4.0     2020.0  2500.0   26.0

正如我们所见,数千个都不起作用。此外,命令并未跳过空白行[1]。

我期望“thousand”命令中出现“,”,并从“skip_blan_lines”命令中删除了 line[1]。

python pandas csv
1个回答
1
投票

thousands
参数是输入文件的属性。它告诉 pandas csv 文件中的数字包含数千个字符(通常是逗号或点)。参数数千不会影响输出。

考虑这段代码:

import pandas as pd
df = pd.read_csv('datafile6.csv', sep=';', thousands=',', skip_blank_lines=True)
print(df)

其中 datafile6.csv 是:

month;day;year;salary;age
8;15;2,012;1,400;25
9;4;2,020;2,500;26

我得到的输出为:

   month   day    year  salary   age
0    8.0  15.0  2012.0  1400.0  25.0
1    9.0   4.0  2020.0  2500.0  26.0

您可以看到 1,400 已被正确解析为 1400 等。

关于您关于skip_blank_lines的问题。我怀疑您的 csv 包含字段分隔符,而不是完全空白的行。
现在将其视为 datafile6.csv 中的内容:

month;day;year;salary;age
8;15;2,012;1,400;25
;;;;
9;4;2,020;2,500;26

9;3;2,021;3,200;33

我得到的数据帧输出为:

   month   day    year  salary   age
0    8.0  15.0  2012.0  1400.0  25.0
1    NaN   NaN     NaN     NaN   NaN
2    9.0   4.0  2020.0  2500.0  26.0
3    9.0   3.0  2021.0  3200.0  33.0

NaN 来自 datafile6.csv 第 3 行,它并不是真正的空白,而是有 4 个字段分隔符。其中完全空白的第 5 行被跳过。这是参数

skip_blank_lines

的行为

希望一切顺利。

© www.soinside.com 2019 - 2024. All rights reserved.