以下是我必须执行的任务的代码段。我想为这项任务提供最好的时间复杂性。是否有图书馆或更好的实施?
import pandas as pd
import numpy as np
df1 = pd.DataFrame({"v_id": [1,1,1,2,2,3,3,3,3,5,5], \
"date": ['01-01-2017', '02-01-2017', '03-01-2017',\
'02-01-2017', '03-01-2017',
'01-01-2017', '02-01-2017', '03-01-2017', '04-01-2017',\
'05-01-2017','04-01-2017'],\
"value":[0.9,1.5,2.4,7.1,0.4,1.8,5.1, 6.4, 7.7, 3.9, 0.3]})
dim1, dim2 = df1.v_id.unique(), df1.date.unique()
dim1_dict, dim2_dict = dict(zip(dim1, list(range(0, len(dim1))))), \
dict(zip(dim2, list(range(0, len(dim2)))))
value_result, date_result = np.empty((len(dim1), len(dim2)), dtype=object),\
np.empty((len(dim1), len(dim2)), dtype=object)
for i in range(0, len(df1)):
date_result[dim1_dict.get(df1.loc[i].v_id), \
dim2_dict.get(df1.loc[i].date)] = df1.loc[i].date
value_result[dim1_dict.get(df1.loc[i].v_id), \
dim2_dict.get(df1.loc[i].date)] = df1.loc[i].value
我的目标是获得 date_result (v_id x 日期)
array([['01-01-2017', '02-01-2017', '03-01-2017', None, None],
[None, '02-01-2017', '03-01-2017', None, None],
['01-01-2017', '02-01-2017', '03-01-2017', '04-01-2017', None],
[None, None, None, '04-01-2017', '05-01-2017']], dtype=object)
&安培; value_result 矩阵(v_id x 值)。
array([[0.90000000000000002, 1.5, 2.3999999999999999, None, None],
[None, 7.0999999999999996, 0.40000000000000002, None, None],
[1.8, 5.0999999999999996, 6.4000000000000004, 7.7000000000000002,
None],
[None, None, None, 0.29999999999999999, 3.8999999999999999]], dtype=object)
答案 0 :(得分:2)
您可以使用pivot_table
执行此计算:
import numpy as np
import pandas as pd
df1 = pd.DataFrame({"v_id": [1,1,1,2,2,3,3,3,3,5,5],
"date": ['01-01-2017', '02-01-2017', '03-01-2017',
'02-01-2017', '03-01-2017',
'01-01-2017', '02-01-2017', '03-01-2017', '04-01-2017',
'05-01-2017','04-01-2017'],
"value":[0.9,1.5,2.4,7.1,0.4,1.8,5.1, 6.4, 7.7, 3.9, 0.3]})
date_result = (df1.assign(date2=df1['date'])
.pivot_table(columns='date', index='v_id',
values='date2', aggfunc='first').values)
value_result = df1.pivot_table(columns='date', index='v_id',
values='value', aggfunc='first').values
print(date_result)
print(value_result)
产生date_result
:
array([['01-01-2017', '02-01-2017', '03-01-2017', None, None],
[None, '02-01-2017', '03-01-2017', None, None],
['01-01-2017', '02-01-2017', '03-01-2017', '04-01-2017', None],
[None, None, None, '04-01-2017', '05-01-2017']], dtype=object)
和<{p>的value_result
array([[ 0.9, 1.5, 2.4, nan, nan],
[ nan, 7.1, 0.4, nan, nan],
[ 1.8, 5.1, 6.4, 7.7, nan],
[ nan, nan, nan, 0.3, 3.9]])
请注意,value_result
是具有浮点dtype的NumPy数组,缺少的值由nan
表示,而不是None
。您可以使用
object
dtype且缺少值None
的NumPy数组
value_result = np.where(pd.isnull(value_result), None, value_result)
产生
array([[0.9, 1.5, 2.4, None, None],
[None, 7.1, 0.4, None, None],
[1.8, 5.1, 6.4, 7.7, None],
[None, None, None, 0.3, 3.9]], dtype=object)