首页 > 解决方案 > 如何使用 pandas 在 python 中重塑 csv 文件?

问题描述

我正在尝试在 python 中重塑这个 csv 文件,使其具有 1,000 行和所有 12 列,但它只显示为一列

我试过使用 df.iloc[:][:1000] 将它缩短到 1000 行,但它仍然只给我 1 列

Wine 的数据框

df_w= pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv') 
df_w= df_w.iloc[:12][:1000]
df_w

显示的数据框是 1000 行,只有 1 列,我想知道如何将数据设置到其各自的列标题中

标签: pythonpandasdataframe

解决方案


使用sep=';', 作为分号分隔符:

df_w= pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv', sep=';')
df_w= df_w.iloc[:12][:1000]
df_w

输出:

    fixed acidity  volatile acidity  citric acid  residual sugar  chlorides  free sulfur dioxide  total sulfur dioxide  density    pH  sulphates  alcohol  quality
0             7.4              0.70         0.00             1.9      0.076                 11.0                  34.0   0.9978  3.51       0.56      9.4        5
1             7.8              0.88         0.00             2.6      0.098                 25.0                  67.0   0.9968  3.20       0.68      9.8        5
2             7.8              0.76         0.04             2.3      0.092                 15.0                  54.0   0.9970  3.26       0.65      9.8        5
3            11.2              0.28         0.56             1.9      0.075                 17.0                  60.0   0.9980  3.16       0.58      9.8        6
4             7.4              0.70         0.00             1.9      0.076                 11.0                  34.0   0.9978  3.51       0.56      9.4        5
5             7.4              0.66         0.00             1.8      0.075                 13.0                  40.0   0.9978  3.51       0.56      9.4        5
6             7.9              0.60         0.06             1.6      0.069                 15.0                  59.0   0.9964  3.30       0.46      9.4        5
7             7.3              0.65         0.00             1.2      0.065                 15.0                  21.0   0.9946  3.39       0.47     10.0        7
8             7.8              0.58         0.02             2.0      0.073                  9.0                  18.0   0.9968  3.36       0.57      9.5        7
9             7.5              0.50         0.36             6.1      0.071                 17.0                 102.0   0.9978  3.35       0.80     10.5        5
10            6.7              0.58         0.08             1.8      0.097                 15.0                  65.0   0.9959  3.28       0.54      9.2        5
11            7.5              0.50         0.36             6.1      0.071                 17.0                 102.0   0.9978  3.35       0.80     10.5        5

推荐阅读