Reputation: 21961
I would like to convert the rows into columns for this dataframe and am therefore using a pivot_table operation in pandas:
However, when I do this:
df_tmp.pivot_table(index='Date', columns='MeasureLabel', values='Value')
I get the error: *** pandas.core.base.DataError: No numeric types to aggregate
. The dtypes are as follows:
Date int64
MeasureLabel object
Value object
The Value
column contains non-numeric values and therefore cannot be made into a number. Is there a way to achieve what I want?
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
Upvotes: 1
Views: 696
Reputation: 164673
Since you aren't looking to perform an aggregation, you can use pd.pivot
instead of pd.pivot_table
. Note pd.pivot
is quite restrictive, e.g. it only allows scalar index
/ columns
, but in this case it seems sufficient.
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
Upvotes: 1