使用熊猫合并相同的列

时间:2021-02-26 01:43:35

标签: python pandas csv merge pandas-groupby

我在 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 的新手。所以只是想知道我如何才能实现?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

由于您的文件有重复的列名,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

答案 2 :(得分:0)

假设 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