我正在处理通过以下方式设置的一些时间序列数据:
indx
列从1开始,直到大约434-460(某些序列比其他序列长),然后再次从1开始。我想做的就是对此进行转置,以便我的数据框看起来像这样,其中每一行都包含时间序列的几个列表:
Time MAG TAU
[time1, time2, ..., timen] [MAG1, MAG2, ..., MAGn] (this is the value stored every time
... ... indx=1)
... ... ...
修改
我现在尝试使用pd.pivot()
而不是melt()
,这与我所获得的尽可能接近。使用:
pd.pivot_table(df, index=['indx'], values=['MAG', 'time'], aggfunc=lambda x:list(x))
但是,这只是一个很长的列表,按indx
分组:
答案 0 :(得分:0)
万一有人遇到类似的情况,我可以通过使用.apply()
在每个时间序列上附加一个数字来解决这个问题,然后用它来索引枢轴:
global count
count = 0
def check_count(ind):
global count
if ind == 1:
count += 1
return count
else:
return count
df['LC'] = df['indx'].apply(lambda x: check_count(x))
df = pd.pivot_table(df, index=['LC'], values=['MAG', 'time'], aggfunc=lambda x: list(x))
df['V'] = df['time'].apply(lambda x: x.pop(0))
df['MAG'].apply(lambda x: x.pop(0));