【问题标题】:Merging columns with same same using pandas使用熊猫合并相同的列
【发布时间】:2021-05-28 10:17:28
【问题描述】:

我在 CSV 文件中有以下数据:

time   conc   time   conc   time    conc   time   conc
1:00    10    5:00   11     9:00    55     13:00   1
2:00    13    6:00   8      10:00   6      14:00   4 
3:00    9     7:00   7      11:00   8      15:00   3
4:00    8     8:00   1      12:00   11     16:00   8

我只是想将它们合并为:

time   conc  
1:00   10
2:00   13
3:00   9
4:00   8
...
16:00  8

我有超过 1000 列,但我是 pandas 的新手。所以只是想知道我如何才能实现?

【问题讨论】:

    标签: python pandas csv merge pandas-groupby


    【解决方案1】:

    一种方法是将数据帧切割成两列切片,然后在重命名后使用 pd.concat() 重新组合。 先正常加载dataframe:

    df = pd.read_csv('time_conc.csv')
    df
    

    看起来像下面这样。注意 pd.read_csv() 为重复的列名添加了后缀:

        time    conc    time.1  conc.1  time.2  conc.2  time.3  conc.3
    0   1:00    10      5:00    11      9:00    55      13:00   1
    1   2:00    13      6:00    8       10:00   6       14:00   4
    2   3:00    9       7:00    7       11:00   8       15:00   3
    3   4:00    8       8:00    1       12:00   11      16:00   8
    

    然后使用 pd.DataFrame.iloc 进行切片:

    total_columns = len(df.columns)
    columns_per_set = 2
    
    column_sets = [df.iloc[:,set_start:set_start + columns_per_set].copy() for set_start in range(0, total_columns, columns_per_set)]
    

    column_sets 现在是一个列表,将每对重复列作为单独的数据框保存。接下来,遍历列表以将列重命名为原始名称:

    for s in column_sets:
        s.columns = ['time', 'conc']
    

    这会修改每个两列数据框,以便它们的列名匹配。 最后使用 pd.concat() 通过匹配列轴来组合它们:

    new_df = pd.concat(column_sets, axis=0, sort=False)
    new_df
    

    这为您提供了完整的两列:

        time    conc
    0   1:00    10
    1   2:00    13
    2   3:00    9
    3   4:00    8
    0   5:00    11
    1   6:00    8
    2   7:00    7
    3   8:00    1
    0   9:00    55
    1   10:00   6
    2   11:00   8
    3   12:00   11
    0   13:00   1
    1   14:00   4
    2   15:00   3
    3   16:00   8
    

    【讨论】:

      【解决方案2】:

      由于您的文件有重复的列名,Pandas 会添加后缀。默认情况下,DataFrame 标头将类似于 ['time', 'conc', 'time.1', 'conc.1', 'time.2', 'conc.2', 'time.3', 'conc. 3' ...]

      假设你的 CSV 文件的分隔符是逗号:

      import pandas as pd
      df = pd.read_csv('/path/to/your/file.csv', sep=',')
      total_n = len(df.columns)
      
      lst = []
      for x in range(int(total_n / 2 )):
          if x == 0:
              cols = ['time', 'conc']
          else:
              cols = ['time'+'.'+str(x), 'conc'+'.'+str(x)]
          df_sub = df[cols]  #Slice two columns each time
          df_sub.columns = ['time', 'conc']  #Slices should have the same column names
          lst.append(df_sub)
      df = pd.concat(lst)  #Concatenate all the objects
      

      【讨论】:

        【解决方案3】:

        假设df 是一个带有 csv 文件数据的 DataFrame,您可以尝试以下操作:

        # rename columns if needed
        df.columns = ["time", "conc"]*(df.shape[1]//2)
        # concatenate pairs of adjacent columns
        pd.concat([df.iloc[:, [i, i+1]] for i in range(0, df.shape[1], 2)])
        

        它给出:

             time conc
        0    1:00  10
        1    2:00  13
        2    3:00   9
        3    4:00   8
        0    5:00  11
        ..    ...  ..
        3   12:00  11
        0   13:00   1
        1   14:00   4
        2   15:00   3
        3   16:00   8
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-03
          • 2021-11-16
          • 2017-04-17
          • 2021-01-08
          • 2022-01-06
          • 2021-12-30
          • 2017-01-31
          • 1970-01-01
          相关资源
          最近更新 更多