Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在单个数据帧的单个列中合并和排序所有索引列_Python_Pandas_Sorting - Fatal编程技术网

Python 在单个数据帧的单个列中合并和排序所有索引列

Python 在单个数据帧的单个列中合并和排序所有索引列,python,pandas,sorting,Python,Pandas,Sorting,我有一个数据框,看起来像这样。它有更多的时间轴,直到时间[s]。30 Time[s] v1 Time[s].1 v2 160.84621 0 160.84808 7 161.14613 0 161.14802 7 161.538245 27 161.540085 7 162.01598 27 162.017865 7 162.31589 27 162.317775 7 162.615855 27 162.617735 7 162.

我有一个数据框,看起来像这样。它有更多的时间轴,直到
时间[s]。30

Time[s]    v1   Time[s].1   v2
160.84621   0   160.84808   7
161.14613   0   161.14802   7
161.538245  27  161.540085  7
162.01598   27  162.017865  7
162.31589   27  162.317775  7
162.615855  27  162.617735  7
162.915765  27  162.91765   7
163.21574   27  163.217625  7
163.51569   27  163.517575  7
163.81563   27  163.81751   7
164.11554   27  164.117425  7
164.4155    27  164.41738   9
164.71543   27  164.717315  9
165.015405  27  165.017285  9
165.31532   27  165.317205  9
165.65083   26  165.65272   9
165.95025   26  165.95214   9
我需要一个时间轴
time[s]。general
,它是具有排序值的所有时间列的合并形式。我已为所有这些列编制了索引

df.set_index(keys=list(file_read.filter(like='Time[s]').columns))
更新:

预期产出:

Time[s]      v1     v2
160.84621   0      null 
160.84808   null     7
160.14613   0      null
161.14802   null     7
161.538245  27     null
161.540085  null     7
162.01598   27     null
162.017865  null     7
162.31589   27     null
162.317775  null     7
等等

更新2:

Time[s]    v1   Time[s].1   v2      Time[s].2   v3
160.84621   0   160.84808   7   158.538395  Active
161.14613   0   161.14802   7   158.538515  Active
161.538245  27  161.540085  7   159.49455   Active
162.01598   27  162.017865  7   162.352395  Locked
162.31589   27  162.317775  7   163.35075   Locked
162.615855  27  162.617735  7   164.350675  Locked
162.915765  27  162.91765   7   165.350655  Locked
163.21574   27  163.217625  7   166.509695  Locked
163.51569   27  163.517575  7   166.509815  Locked
163.81563   27  163.81751   7   167.50086   Locked
164.11554   27  164.117425  7   168.50085   Locked
164.4155    27  164.41738   9   169.500865  Locked
164.71543   27  164.717315  9   171.502655  Standby
165.015405  27  165.017285  9   185.89923   Forward
165.31532   27  165.317205  9   3273.448065 Forward
165.65083   26  165.65272   9   3274.43487  Forward
165.95025   26  165.95214   9   3275.4348   Forward
我认为需要:

b  = df.filter(like='v').columns

d = {x: 'v.{}'.format(i) for i, x in enumerate(b)}
d['Time[s]'] = 'Time[s].0'
print (d)
{'v1': 'v0', 'v2': 'v1', 'Time[s]': 'Time[s].0'}

df = df.rename(columns=d)
L = [x.set_index(x.columns[0]) for i, x in df.groupby(lambda x: x.split('.')[-1], axis=1)]
df = pd.concat(L, axis=1)
print (df.head(10))
             v.0  v.1
160.846210   0.0  NaN
160.848080   NaN  7.0
161.146130   0.0  NaN
161.148020   NaN  7.0
161.538245  27.0  NaN
161.540085   NaN  7.0
162.015980  27.0  NaN
162.017865   NaN  7.0
162.315890  27.0  NaN
162.317775   NaN  7.0
消除

  • 第一列为字典的所有
    v
    列,用于成对时间戳和值列
  • 通过
    dict
    重命名
    ,也是第一个
    时间戳
  • groupby
    按列表理解中
    之后的列A的值创建索引
  • 编辑:

    如果数值和重复时间戳聚合是通过
    平均值进行的,如果不是,则通过
    第一次进行聚合:

    b  = df.filter(like='v').columns
    
    d = {x: 'v.{}'.format(i) for i, x in enumerate(b)}
    d['Time[s]'] = 'Time[s].0'
    print (d)
    {'v1': 'v0', 'v2': 'v1', 'Time[s]': 'Time[s].0'}
    
    df = df.rename(columns=d)
    L = [x.groupby(x.columns[0]).mean() 
         if np.issubdtype(df[x.columns[1]].dtype, np.number)
         else x.groupby(x.columns[0]).first() 
         for i, x in df.groupby(df.columns.str.split('.').str[-1], axis=1)]
    
    df = pd.concat(L, axis=1)
    print (df.head(10))
                 v.0  v.1     v.2
    158.538395   NaN  NaN  Active
    158.538515   NaN  NaN  Active
    159.494550   NaN  NaN  Active
    160.846210   0.0  NaN     NaN
    160.848080   NaN  7.0     NaN
    161.146130   0.0  NaN     NaN
    161.148020   NaN  7.0     NaN
    161.538245  27.0  NaN     NaN
    161.540085   NaN  7.0     NaN
    162.015980  27.0  NaN     NaN
    

    我已经尝试了第一种解决方案,但我需要一些不同的解决方案。时间轴很完美,但我不想将所有
    v
    列放在一列中。我想把它们放在那里。在新的时间轴中进行排序后,我希望所有
    v
    列都相应地排列。我将更新问题中的预期输出。当
    v
    值不存在时,我想将
    null
    @tworitdash-请检查完全更改的解决方案。我在
    df=pd.concat(L,axis=1)
    处得到一个错误,它说
    重新索引仅对唯一值的索引对象有效
    @tworitdash-我发现输入错误,需要
    d={x:'v.{}。格式(I)对于枚举(b)}中的i,x
    instaed
    d={x:'v{}。枚举(b)}中的i,x的格式(i)有效。完成!非常感谢。