pd.series更改列的值

时间:2017-02-24 15:26:06

标签: python pandas

我正在尝试从包含的csv创建日历热图 雨天(VM)和日期(ISTANTE)。

IDSTAZ,SENSORE,ISTANTE,VM
56,300001896,201601010000,0.0
56,300001896,201601020000,0.2
56,300001896,201601030000,0.6
56,300001896,201601040000,1.8

在网上搜索我写了以下代码:

import pandas as pd
import calmap
import matplotlib.pyplot as plt

df = pd.read_csv('D:\\GIS\\Dati\\Meteo_arpav\\stazione_forno.csv')
df['DateTime'] = df['ISTANTE'].apply(lambda x: pd.to_datetime(str(x), format='%Y%m%d%H%M'))
events = pd.Series((df['VM']), index=df['DateTime'])
a=calmap.calendarplot(events)
plt.show(a)

虽然有效,但是当我创建series时,VM的所有值都变为NaN。 打印(事件)结果如下:

DateTime
2016-01-01   NaN
2016-01-02   NaN
2016-01-03   NaN
2016-01-04   NaN
2016-01-05   NaN
2016-01-06   NaN
2016-01-07   NaN
2016-01-08   NaN
2016-01-09   NaN

通过这种方式,日历热图显示为空。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您似乎需要set_index才能创建Series

df['DateTime'] = pd.to_datetime(df['ISTANTE'], format='%Y%m%d%H%M')
events = df.set_index('DateTime')['VM']
print (events)
DateTime
2016-01-01    0.0
2016-01-02    0.2
2016-01-03    0.6
2016-01-04    1.8
Name: VM, dtype: float64

无法工作的原因:

events = pd.Series((df['VM']), index=df['DateTime'])

是数据对齐。列VMSeries)的索引想要按DatetimeIndex对齐,但值不匹配,因此结果为NaN s。