簡體   English   中英

基於列重復DataFrame中的行

[英]Repeating rows in a DataFrame based on a column

我現在有一個數據幀:

   class1  class2  value  value2
0       1       0      1       4
1       2       1      2       3
2       2       0      3       5
3       3       1      4       6

我想重復行並根據valuevalue2之間的差異插入相同數量的增量列。 我想得到數據幀應該是這樣的:

    class1  class2  value  value2  value3
0        1       0      1       4       1
1        1       0      1       4       2
2        1       0      1       4       3
3        1       0      1       4       4
4        2       1      2       3       2
5        2       1      2       3       3
6        2       0      3       5       3
7        2       0      3       5       4
8        2       0      3       5       5
9        3       1      4       6       4
10       3       1      4       6       5
11       3       1      4       6       6

我試過它:

def func(x):
    copy = x.copy()
    num = x.value2+1-x.value
    return pd.concat([copy]*num.values[0])
df= df.groupby(['class1','class2']).apply(lambda x:func(x))

但會有一個oredr的問題,使我不知道如何添加列value3 而且我希望有一種優雅的方式來做到這一點。

誰能幫我? 提前致謝

計算差異並調用Index.repeat

idx = df.index.repeat(df.value2 - df.value + 1)

現在,要么使用reindex

df = df.reindex(idx).reset_index(drop=True)

或者loc

df = df.loc[idx].reset_index(drop=True)

你得到了

df
    class1  class2  value  value2
0        1       0      1       4
1        1       0      1       4
2        1       0      1       4
3        1       0      1       4
4        2       1      2       3
5        2       1      2       3
6        2       0      3       5
7        2       0      3       5
8        2       0      3       5
9        3       1      4       6
10       3       1      4       6
11       3       1      4       6

對於問題的第二部分,您需要groupby.cumcount

s = idx.to_series()
df['value3'] =  df['value'] + s.groupby(idx).cumcount().values
df
    class1  class2  value  value2  value3
0        1       0      1       4       1
1        1       0      1       4       2
2        1       0      1       4       3
3        1       0      1       4       4
4        2       1      2       3       2
5        2       1      2       3       3
6        2       0      3       5       3
7        2       0      3       5       4
8        2       0      3       5       5
9        3       1      4       6       4
10       3       1      4       6       5
11       3       1      4       6       6

這是一系列可以獲得所需輸出的東西:

df.join(df
        .apply(lambda x: pd.Series(range(x.value, x.value2+1)), axis=1)
        .stack().astype(int)
        .reset_index(level=1, drop=1)
        .to_frame('value3')).reset_index(drop=1)

Out[]:
    class1  class2  value  value2  value3
0        1       0      1       4       1
1        1       0      1       4       2
2        1       0      1       4       3
3        1       0      1       4       4
4        2       1      2       3       2
5        2       1      2       3       3
6        2       0      3       5       3
7        2       0      3       5       4
8        2       0      3       5       5
9        3       1      4       6       4
10       3       1      4       6       5
11       3       1      4       6       6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM