我试图将pandas数据帧中的单元格提取为简单的浮点数。我正在尝试
prediction = pd.to_numeric(baseline.ix[(baseline['Weekday']==5) & (baseline['Hour'] == 8)]['SmsOut'])
然而,这会返回
128 -0.001405
Name: SmsOut, dtype: float64
我希望它只返回一个simle Python float:-0.001405
我怎么能这样做?
答案 0 :(得分:1)
输出为Series
,只有一个值,因此更有可能是解决方案:
Series.item
values
转换为numpy array
并通过索引选择第一个值iloc
或iat
prediction = pd.to_numeric(baseline.loc[(baseline['Weekday'] ==5 ) &
(baseline['Hour'] == 8), 'SmsOut'])
print (prediction.item())
print (prediction.values[0])
print (prediction.iloc[0])
print (prediction.iat[0])
样品:
baseline = pd.DataFrame({'Weekday':[5,3],
'Hour':[8,4],
'SmsOut':[-0.001405,6]}, index=[128,130])
print (baseline)
Hour SmsOut Weekday
128 8 -0.001405 5
130 4 6.000000 3
prediction = pd.to_numeric(baseline.loc[(baseline['Weekday'] ==5 ) &
(baseline['Hour'] == 8), 'SmsOut'])
print (prediction)
128 -0.001405
Name: SmsOut, dtype: float64
print (prediction.item())
-0.001405
print (prediction.values[0])
-0.001405
print (prediction.iloc[0])
-0.001405
print (prediction.iat[0])
-0.001405