执行Pandas枢纽分析表作业时,没有要汇总的数值类型

时间:2018-10-11 23:44:44

标签: python pandas

我想将此数据帧的行转换为列,因此在熊猫中使用了pivot_table操作:

但是,当我这样做时: df_tmp.pivot_table(index='Date', columns='MeasureLabel', values='Value'),我得到了错误:*** pandas.core.base.DataError: No numeric types to aggregate。 dtype如下:

Date             int64
MeasureLabel    object
Value           object

Value列包含非数字值,因此无法将其转换为数字。有什么方法可以实现我想要的?

          Date                                MeasureLabel             Value
0     1539262800000000000                    Airpressure_Hourly (hPa)  1008.0
1     1539262800000000000                     Cloudcover_Hourly (pct)       1
2     1539262800000000000                         Dewpoint_Hourly (C)    15.2
3     1539262800000000000           GlobalRadiation_HourlySum (Wh/m2)     259
4     1539262800000000000                    HumidityRel_Hourly (pct)      33
5     1539262800000000000           Lightintensity_Hourly (µmol/m²/s)   569.1
6     1539262800000000000                          PAR_Hourly (Wh/m2)   124.5
7     1539262800000000000                            PictoCode_Hourly      13
8     1539262800000000000                       Precip_HourlySum (mm)    0.00
9     1539262800000000000              Precip_RangeMax_HourlySum (mm)     0.0
10    1539262800000000000              Precip_RangeMin_HourlySum (mm)     0.0
11    1539262800000000000              PrecipProbability_Hourly (pct)       0
12    1539262800000000000  Referenceevapotranspiration_HourlySum (mm)     0.2
13    1539262800000000000              ShowerProbability_Hourly (pct)       0
14    1539262800000000000                         SnowFraction_Hourly     0.0
15    1539262800000000000          Soilmoisture_0to10cm_Hourly (vol%)     7.0
16    1539262800000000000          Soiltemperature_0to10cm_Hourly (C)     7.0
17    1539262800000000000               SunshineDuration_Hourly (min)      59
18    1539262800000000000                          TempAir_Hourly (C)    33.7
19    1539262800000000000                    TempAirSurface_Hourly(C)    39.0
20    1539262800000000000        ThunderstormProbability_Hourly (pct)      55
21    1539262800000000000                       Visibility_Hourly (m)   35830
22    1539262800000000000                        WindDirection_Hourly       E

1 个答案:

答案 0 :(得分:1)

由于您不想执行聚合,因此可以使用pd.pivot代替pd.pivot_table。注意pd.pivot的限制非常严格,例如它只允许标量index / columns,但在这种情况下,似乎就足够了。

df = pd.DataFrame({'Date': ['20180101']*5,
                   'Label': ['A', 'B', 'C', 'D', 'E'],
                   'Value': [1, 2, 3, 4, 'X']})

res = df.pivot(index='Date', columns='Label', values='Value')

print(res)

Label     A  B  C  D  E
Date                   
20180101  1  2  3  4  X