Pandas:当存在空白数据项时,如何读取数据文件并对齐列?

2024-09-28 22:40:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有此文件,导入时:

reading     depth       cone   friction       pore    slope_X    slope_Y
    2934     0.001     0.0009                            -0.17      -0.49
    2935     0.007     0.0014               -0.0001      -0.19      -0.47
    2936     0.014     0.0012               -0.0003      -0.21      -0.45
    2937     0.021     0.0009                0.0001      -0.19      -0.48
    2938     0.029     0.0017                0.0002      -0.16      -0.42
    2939     0.041     0.0015                0.0005      -0.17      -0.44
    2940     0.052     0.0017                0.0003      -0.18      -0.52
    2941     0.065     0.0017                0.0002      -0.16      -0.45
    2942     0.078     0.0020                0.0007      -0.15      -0.48
    2943     0.090     0.0021                0.0008      -0.16      -0.47
    2944     0.099     0.0019                0.0011      -0.24      -0.42
    2945     0.109     0.0024                0.0009      -0.22      -0.45
    2946     0.119     0.0029                0.0008      -0.20      -0.48
    2947     0.130     0.0033                0.0013      -0.17      -0.43
    2948     0.140     0.0030                0.0015      -0.22      -0.41
    2949     0.151     0.0030                0.0011      -0.19      -0.47
    2950     0.162     0.0046     0.0000     0.0014      -0.18      -0.46
    2951     0.173     0.0049     0.0001     0.0018      -0.16      -0.44
    2952     0.183     0.0055     0.0001     0.0018      -0.13      -0.43
    2953     0.193     0.0064     0.0001     0.0017      -0.20      -0.45
    2954     0.204     0.0067     0.0001     0.0018      -0.22      -0.46
    2955     0.215     0.0073     0.0001     0.0018      -0.20      -0.49
    2956     0.226     0.0065     0.0002     0.0020      -0.12      -0.46
    2957     0.236     0.0075     0.0002     0.0024      -0.08      -0.45
    2958     0.247     0.0076     0.0002     0.0021      -0.12      -0.45
    2959     0.258     0.0084     0.0002     0.0024      -0.15      -0.46
    2960     0.268     0.0097     0.0002     0.0026      -0.16      -0.49

当我使用df=pd.read_csv(filename, delim_whitespace=True)时看起来是这样的

它会移动下一列的值,这是我不想做的。 我想读取每列的数据。你知道我怎么做吗

     reading  depth     cone  friction    pore  slope_x  slope_y
0       2933  0.000   0.0010   -0.1900 -0.4800      NaN      NaN
1       2934  0.001   0.0009   -0.1700 -0.4900      NaN      NaN
2       2935  0.007   0.0014   -0.0001 -0.1900    -0.47      NaN
3       2936  0.014   0.0012   -0.0003 -0.2100    -0.45      NaN
4       2937  0.021   0.0009    0.0001 -0.1900    -0.48      NaN
..       ...    ...      ...       ...     ...      ...      ...
486     3930  4.351  17.6757    0.0183 -0.0179    -0.16    -0.17
487     3931  4.353  18.1356    0.0193 -0.0184    -0.17    -0.15
488     3932  4.356  18.3872    0.0206 -0.0287    -0.19    -0.19
489     3933  4.357  18.3862    0.0214 -0.0278    -0.13    -0.15
490     3934  4.357  18.5794    0.0230 -0.0300    -0.17    -0.12

使用df = pd.read_csv(filename, sep='\r\t')时返回: 因此数据与列不匹配。我们的想法是用NaN来代替空白

enter image description here


Tags: 文件csv数据dfreadnanfilenameslope
1条回答
网友
1楼 · 发布于 2024-09-28 22:40:38

问题似乎是您的列缺少数据条目,因此当Pandas使用所有空白作为分隔符解析数据文件时,这些空白部分最终会使列偏离对齐

检查数据文件,查看列是否用空格、制表符或其他字符分隔。例如,如果分隔符为制表符,则可以将代码更改为:

df = pd.read_csv(filename, sep='\r\t')

如果每列之间有多个空格,那么sep可以设置为:

df = pd.read_csv(filename, sep='   ')  # Needs to exactly match # of spaces between columns.

您可能还希望指定数据文件的encoding,例如:

df = pd.read_csv(filename, sep='\r\t', encoding='utf-8')

相关问题 更多 >