Python 基于列的日期和值重新索引数据帧
假设我有以下数据帧Python 基于列的日期和值重新索引数据帧,python,pandas,Python,Pandas,假设我有以下数据帧 ID StationID Date ParamName ParamValue 0 A 1990-01-08 metal 0.5 1 A 1990-01-08 wood 1.4 2 A 1990-01-08 glass 9.7 3 B 1990-01-08 metal 0.8 4 B 199
ID StationID Date ParamName ParamValue
0 A 1990-01-08 metal 0.5
1 A 1990-01-08 wood 1.4
2 A 1990-01-08 glass 9.7
3 B 1990-01-08 metal 0.8
4 B 1990-01-08 wood 4.8
5 C 1990-01-08 metal 0.6
6 A 1990-02-03 metal 0.5
7 A 1990-03-01 metal 1.2
8 B 1990-03-01 metal 0.9
9 C 1990-03-01 metal 1.1
仅当日期
或StationID
不同时,您如何将数据帧的ID列重新索引为增量。如何将上面的数据帧重新索引到下面的数据帧(假设日期包含datetime对象)
这是你需要的吗
df.assign(ID=(df.StationID!=df.StationID.shift()).cumsum()-1)
Out[151]:
ID StationID Date ParamName ParamValue
0 0 A 1990-01-08 metal 0.5
1 0 A 1990-01-08 wood 1.4
2 0 A 1990-01-08 glass 9.7
3 1 B 1990-01-08 metal 0.8
4 1 B 1990-01-08 wood 4.8
5 2 C 1990-01-08 metal 0.6
6 3 A 1990-02-03 metal 0.5
7 3 A 1990-02-03 wood 1.2
8 4 B 1990-02-03 metal 0.9
9 5 C 1990-02-03 metal 1.1
更新:-)
Think Char说他们需要根据StationID或日期进行更改
df.assign(ID=(df.StationID!=df.StationID.shift()).cumsum()-1)
Out[151]:
ID StationID Date ParamName ParamValue
0 0 A 1990-01-08 metal 0.5
1 0 A 1990-01-08 wood 1.4
2 0 A 1990-01-08 glass 9.7
3 1 B 1990-01-08 metal 0.8
4 1 B 1990-01-08 wood 4.8
5 2 C 1990-01-08 metal 0.6
6 3 A 1990-02-03 metal 0.5
7 3 A 1990-02-03 wood 1.2
8 4 B 1990-02-03 metal 0.9
9 5 C 1990-02-03 metal 1.1
df['ID']=df.StationID+df.Date.astype(str)
df.assign(ID=(df.ID!=df.ID.shift()).cumsum()-1)
Out[163]:
ID StationID Date ParamName ParamValue
0 0 A 1990-01-08 metal 0.5
1 0 A 1990-01-08 wood 1.4
2 0 A 1990-01-08 glass 9.7
3 1 B 1990-01-08 metal 0.8
4 1 B 1990-01-08 wood 4.8
5 2 C 1990-01-08 metal 0.6
6 3 A 1990-02-03 metal 0.5
7 4 A 1990-03-01 metal 1.2
8 5 B 1990-03-01 metal 0.9
9 6 C 1990-03-01 metal 1.1