我有以下數據框:移調和擴大數據
id begcost endcost
100 1 3
200 10 12
我想:
id newcost
100 1
100 2
100 3
200 10
200 11
200 12
基本上我需要創建包含在begcost和endcost範圍內的每個值的新行列。已嘗試多個轉置選項,但似乎無法擊中我所需要的TIA。
我有以下數據框:移調和擴大數據
id begcost endcost
100 1 3
200 10 12
我想:
id newcost
100 1
100 2
100 3
200 10
200 11
200 12
基本上我需要創建包含在begcost和endcost範圍內的每個值的新行列。已嘗試多個轉置選項,但似乎無法擊中我所需要的TIA。
pd.DataFrame(
[(i, j) for i, b, e in df.itertuples(index=False) for j in range(b, e + 1)],
columns=['id', 'newcost']
)
id newcost
0 100 1
1 100 2
2 100 3
3 200 10
4 200 11
5 200 12
定時
%%timeit
(df.set_index('id').apply(lambda x: pd.Series(np.arange(x.iloc[0],x.iloc[1]+1)), axis=1)
.reset_index()
.melt(id_vars='id')
.drop('variable', axis=1)
.rename(columns={'value':'newcost'}))
100 loops, best of 3: 3.03 ms per loop
%%timeit
pd.DataFrame(
[(i, j) for i, b, e in df.itertuples(index=False) for j in range(b, e + 1)],
columns=['id', 'newcost']
)
1000 loops, best of 3: 1.01 ms per loop
df_out = (df.set_index('id').apply(lambda x: pd.Series(np.arange(x.iloc[0],x.iloc[1]+1)), axis=1)
.reset_index()
.melt(id_vars='id')
.drop('variable', axis=1)
.rename(columns={'value':'newcost'}))
輸出:
id newcost
0 100 1
1 200 10
2 100 2
3 200 11
4 100 3
5 200 12
現在,這是一些性能改進!在今天的選票中,明天你會得到我的讚賞。 –
@ScottBoston只要我有你的尊重......這就是我需要的一切: - )...但我會參加投票,當然! – piRSquared