我正在处理一个大型数据集。我从数据集中的表中提取所有数据,如下所示(输出1):
Label Value
Time
2010-01-01 00:00:30.560 AAAAA [3]
2010-01-01 00:00:30.560 BB -2.6000
2010-01-01 00:00:30.560 C -2.2000
2010-01-01 00:00:30.560 DD 0.0000
2010-01-01 00:00:40.160 GG 1_2_3
我的目标是将其更改为并取出虚拟变量Label和value,并将时间作为索引
Time AAAA BB C DD GG
2010-01-01 00:00:30.560 3 -2.6 -2.2 0 NaN
2010-01-01 00:00:40.160 NaN NaN NaN NaN 1_2_3
我的代码如下所示:
logparser = parse_filter_logfile('CleanLog2.txt')
df = pd.DataFrame(logparser, columns = ['Time', 'Label', 'Value'])
df['Time'] = pd.to_datetime(df['Time'], format="%Y%m%d_%H:%M:%S.%f") #I get Output 1 here
H=df.set_index(['Time', 'Label']).unstack(-1) #error I get the error: Index contains duplicate entries, cannot reshape
我尝试了几件事,但是没有任何运气。当我使用这个我得到这个错误
df.pivot(index='Time', columns='Code') #Error: Index contains duplicate entries, cannot reshape
当我使用这个我得到这个错误:
df.pivot(index='Time', columns='Code',aggfunc='mean') #pivot() got an unexpected keyword argument 'aggfunc'
如果有人可以提供帮助,我将不胜感激。提前致谢
我使用的更新
H=df.pivot_table(index='Time', columns='Code', values='Value',aggfunc='first')
这样就解决了问题。
您要使用pivot_table而不是pivot。没有示例数据很难说,但这应该可行
pd.pivot_table(df,values="Value",index='Time', columns='Code',aggfunc='mean')
如果您有非数字数据,则应单独处理,然后将其与数字数据合并。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句