对于行和列的某些子集,如何将列转换为行?

时间:2019-11-10 17:01:33

标签: python dataframe

您如何使用我的数据将行转换为列?我当前的数据集看起来像下面显示的“原始df”,我希望它看起来像“新df2”。要明确一点,约会1与SDAP1和RDAP1匹配,因此约会2对应于SDAP2 RDAP2。

原始df:

Name    Appoint1    Appoint2    Appoint1t    Appoint2t   SDAP1    RDAP1    SDAP2    RDAP2     

Sam     23.09.2017  24.09.2017  11:00:00     11:00:00    3        -9        6        8
Sarah   24.09.2017  27.09.2017  12:00:00     12:00:00    2        Nan       7        8
Steve   23.10.2017  31.10.2017  11:00:00     12:00:00    5         9        7        9
Mark    23.09.2017              11:00:00                 0         3        
James   23.09.2017  26.09.2017               11:00:00    4         7        1        4

新df:

Name    Appointments    Appointmenttime    SDAP    RDAP

Sam     23.09.2017      11:00:00           3       -9
Sam     24.09.2017      11:00:00           6        8
Sarah   24.09.2017      12:00:00           2        NaN
Sarah   27.09.2017      12:00:00           7        8
Steve   23.10.2017      11:00:00           5        9
Steve   31.10.2017      12:00:00           7        9
Mark    23.09.2017      11:00:00           0        3
James   23.09.2017                         4        7
James   26.09.2017      11:00:00           1        4

1 个答案:

答案 0 :(得分:2)

是否有必要使用wide_to_long?使用concat似乎要容易得多。

df1 = df[["Name","Appoint1","Appoint1t"]]
df2 = df[["Name","Appoint2","Appoint2t"]].rename(columns={"Appoint2": "Appoint1", "Appoint2t": "Appoint1t"})

print (pd.concat([df1,df2]).dropna().sort_index())

#
    Name    Appoint1 Appoint1t
0    Sam  23.09.2017  11:00:00
0    Sam  24.09.2017  11:00:00
1  Sarah  24.09.2017  12:00:00
1  Sarah  27.09.2017  12:00:00
2  Steve  23.10.2017  11:00:00
2  Steve  31.10.2017  12:00:00
3   Mark  23.09.2017  11:00:00
4  James  23.09.2017  11:00:00
4  James  26.09.2017  11:00:00

首先重命名各列,以使用wide_to_long

df.columns = ['Name', 'Appoint_1', 'Appoint_2', 'Time_1', 'Time_2']

print (pd.wide_to_long(df,stubnames=["Appoint","Time"],i="Name",j="count",sep='_')
       .dropna().reset_index().drop("count",axis=1))

#
    Name     Appoint      Time
0    Sam  23.09.2017  11:00:00
1  Sarah  24.09.2017  12:00:00
2  Steve  23.10.2017  11:00:00
3   Mark  23.09.2017  11:00:00
4  James  23.09.2017  11:00:00
5    Sam  24.09.2017  11:00:00
6  Sarah  27.09.2017  12:00:00
7  Steve  31.10.2017  12:00:00
8  James  26.09.2017  11:00:00