Python: Yinelenen değerlere sahip aynı anda birden çok sütun için Pandalar pivot tablosu

0

Soru

sütun adı, okul ve işaretleri olan bir pandalar veri çerçevesine sahip olun

name  school  marks

tom     HBS     55
tom     HBS     55
tom     HBS     14
mark    HBS     28
mark    HBS     19
lewis   HBS     88

Bu şekilde nasıl aktarılır ve dönüştürülür

name  school  marks_1 marks_2 marks_3

tom     HBS     55     55       14
mark    HBS     28     19
lewis   HBS     88

bunu denedim:

df = df.pivot_table(index='name', values='marks', columns='school') \
    .reset_index() \
    .rename_axis(None, axis=1)

print(df)
df = df.pivot('name','marks','school')

bu bağlantıları kontrol ettim

https://stackoverflow.com/questions/22798934/pandas-long-to-wide-reshape-by-two-variables
https://stackoverflow.com/questions/62391419/pandas-group-by-and-convert-rows-into-multiple-columns
https://stackoverflow.com/questions/60698109/pandas-multiple-rows-to-single-row-with-multiple-columns-on-2-indexes

yinelenen değerler nedeniyle bu hatayı alıyorum. yinelenen varsa ve bunları saklamamız gerekiyorsa nasıl başa çıkılır

ValueError: Index contains duplicate entries, cannot reshape
dataframe group-by pandas pivot
2021-11-23 02:17:12
2

En iyi cevabı

2

Kullanmayı deneyin set_index ve unstack ile groupby ve cumcount:

df_out = df.set_index(['name',
                       'school',
                       df.groupby(['name','school'])\
           .cumcount() +1]).unstack()
df_out.columns = [f'{i}_{j}' for i, j in df_out.columns]
df_out = df_out.reset_index()
df_out

Çıktı:

    name school  marks_1  marks_2  marks_3
0  lewis    HBS     88.0      NaN      NaN
1   mark    HBS     28.0     19.0      NaN
2    tom    HBS     55.0     55.0     14.0
2021-11-23 02:27:52
1

Bu cumcount işlev, döndürmeden önce benzersiz dizinler oluşturmanıza olanak tanır. Bu, @ ScottBoston ile aynı fikre dayanır; Ancak, pivot işlev burada kullanılır:

index = ['name', 'school']

                  # create an extra column for uniqueness          
temp = (df.assign(counter = df.groupby(index)
                              .cumcount()
                              .add(1)
                              .astype(str))
          .pivot(index = index, columns = 'counter')
        )

# flatten the columns
temp.columns = temp.columns.map('_'.join)

temp.reset_index()

    name school  marks_1  marks_2  marks_3
0  lewis    HBS     88.0      NaN      NaN
1   mark    HBS     28.0     19.0      NaN
2    tom    HBS     55.0     55.0     14.0

Alternatif olarak, etrafında sözdizimsel şeker olan pyjanitor'dan pivot_wider işlevini kullanabilirsiniz pd.pivot. bazı yardımcılarla:

# pip install pyjanitor
import pandas as pd
import janitor
(df.assign(counter = df.groupby(index)
                       .cumcount()
                       .add(1))                              
   .pivot_wider(index = index, 
                names_from = 'counter', 
                names_sep = '_')
)

    name school  marks_1  marks_2  marks_3
0  lewis    HBS     88.0      NaN      NaN
1   mark    HBS     28.0     19.0      NaN
2    tom    HBS     55.0     55.0     14.0
2021-11-23 03:14:53

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................