如何在包含最大值的系列的 Python 和 return 索引中使用 zip 函数?
How can I use zip function in Python and return index of the series containing max value?
我正在使用 zip 比较两个系列 Max_Plot2015_serie、Max_Plot2005_2014_serie 和 return 两个系列的最大值 Max_scatter2015['Temp_Celcius'].
我怎样才能导入相应值的索引(索引是日期)?
我是新手Python,对功能掌握不是很好
Max_scatter2015['Temp_Celcius'] = [max(value) for value in zip(Max_Plot2015_serie, Max_Plot2005_2014_serie)]
Max_Plot2005_2014_serie
2014-12-25 10.0
2014-12-26 10.0
2014-12-27 11.1
2014-12-28 13.3
2014-12-30 3.3
2014-12-31 -2.8
Name: Temp_Celcius, dtype: float64
<class 'pandas.core.series.Series'>
Max_Plot2015_serie
2015-10-02 18.9
2015-03-10 9.4
2015-02-23 -1.1
2015-06-09 25.6
Name: Temp_Celcius, dtype: float64
<class 'pandas.core.series.Series'>
Max_scatter2015
Temp_Celcius [18.9, 13.9, 26.1, 23.3, 6.7, 18.3, 27.8, 7.2,...
dtype: object
<class 'pandas.core.series.Series'>
也许这就是您要找的:
df = pandas.DataFrame([])
df["Max_Plot2015_serie"] = Max_Plot2015_serie
df["Max_Plot2005_2014_serie"] = Max_Plot2005_2014_serie
df["max_value"] = df.apply(lambda x: max(x.Max_Plot2015_serie, x.Max_Plot2005_2014_serie), axis=1)
df["max_index"] = df.apply(lambda x: (0 if x.Max_Plot2015_serie > x.Max_Plot2005_2014_serie else 1), axis=1)
IIUC,你可以这样试试:
s_2014 = pd.Series(np.random.randint(0,120,365),
index=pd.date_range('2014-01-01', periods=365, freq='D'))
s_2015 = pd.Series(np.random.randint(0,135, 365),
index=pd.date_range('2015-01-01', periods=365, freq='D'))
按天查找最大值:
m = [max(i, j, key=lambda x: x[1]) for i, j in zip(s_2014.iteritems(), s_2015.iteritems())]
j, i = zip(*m)
s = pd.Series(i, index=j)
s
输出:
2015-01-01 104
2014-01-02 101
2014-01-03 118
2014-01-04 26
2015-01-05 98
...
2015-12-27 132
2014-12-28 58
2015-12-29 110
2015-12-30 115
2015-12-31 35
Length: 365, dtype: int64
仅更新 2015 年数据:
s[s.index.year == 2015]
输出:
2015-01-01 60
2015-01-03 109
2015-01-06 71
2015-01-07 113
2015-01-09 9
...
2015-12-16 90
2015-12-23 98
2015-12-26 132
2015-12-27 107
2015-12-29 65
Length: 204, dtype: int64
我正在使用 zip 比较两个系列 Max_Plot2015_serie、Max_Plot2005_2014_serie 和 return 两个系列的最大值 Max_scatter2015['Temp_Celcius']. 我怎样才能导入相应值的索引(索引是日期)? 我是新手Python,对功能掌握不是很好
Max_scatter2015['Temp_Celcius'] = [max(value) for value in zip(Max_Plot2015_serie, Max_Plot2005_2014_serie)]
Max_Plot2005_2014_serie
2014-12-25 10.0
2014-12-26 10.0
2014-12-27 11.1
2014-12-28 13.3
2014-12-30 3.3
2014-12-31 -2.8
Name: Temp_Celcius, dtype: float64
<class 'pandas.core.series.Series'>
Max_Plot2015_serie
2015-10-02 18.9
2015-03-10 9.4
2015-02-23 -1.1
2015-06-09 25.6
Name: Temp_Celcius, dtype: float64
<class 'pandas.core.series.Series'>
Max_scatter2015
Temp_Celcius [18.9, 13.9, 26.1, 23.3, 6.7, 18.3, 27.8, 7.2,...
dtype: object
<class 'pandas.core.series.Series'>
也许这就是您要找的:
df = pandas.DataFrame([])
df["Max_Plot2015_serie"] = Max_Plot2015_serie
df["Max_Plot2005_2014_serie"] = Max_Plot2005_2014_serie
df["max_value"] = df.apply(lambda x: max(x.Max_Plot2015_serie, x.Max_Plot2005_2014_serie), axis=1)
df["max_index"] = df.apply(lambda x: (0 if x.Max_Plot2015_serie > x.Max_Plot2005_2014_serie else 1), axis=1)
IIUC,你可以这样试试:
s_2014 = pd.Series(np.random.randint(0,120,365),
index=pd.date_range('2014-01-01', periods=365, freq='D'))
s_2015 = pd.Series(np.random.randint(0,135, 365),
index=pd.date_range('2015-01-01', periods=365, freq='D'))
按天查找最大值:
m = [max(i, j, key=lambda x: x[1]) for i, j in zip(s_2014.iteritems(), s_2015.iteritems())]
j, i = zip(*m)
s = pd.Series(i, index=j)
s
输出:
2015-01-01 104
2014-01-02 101
2014-01-03 118
2014-01-04 26
2015-01-05 98
...
2015-12-27 132
2014-12-28 58
2015-12-29 110
2015-12-30 115
2015-12-31 35
Length: 365, dtype: int64
仅更新 2015 年数据:
s[s.index.year == 2015]
输出:
2015-01-01 60
2015-01-03 109
2015-01-06 71
2015-01-07 113
2015-01-09 9
...
2015-12-16 90
2015-12-23 98
2015-12-26 132
2015-12-27 107
2015-12-29 65
Length: 204, dtype: int64