Решения для сводной колонны Phone Number
:
g = df.groupby('ID').cumcount().add(1)
df1 = df.set_index([g, 'ID'])['Phone Number'].unstack().add_prefix('Phone Number ')
print (df1)
ID Phone Number 1 Phone Number 2
1 234444.0 30909.0
2 989898.0 NaN
Или:
df['idx'] = df.groupby('ID').cumcount().add(1)
df1 = df.pivot('idx', 'ID', 'Phone Number').add_prefix('Phone Number ')
print (df1)
ID Phone Number 1 Phone Number 2
idx
1 234444.0 30909.0
2 989898.0 NaN
Или:
s = df.groupby('ID')['Phone Number'].apply(list)
df1 = pd.DataFrame(s.values.tolist(), index=s.index).add_prefix('Phone Number ')
print (df1)
Phone Number 0 Phone Number 1
ID
1 234444 989898.0
2 30909 NaN
Последнее, если нужно индексировать столбец для решений выше:
df1 = df1.rename_axis(None, axis=1).rename_axis('ID').reset_index()
print (df1)
ID Phone Number 1 Phone Number 2
0 1 234444.0 30909.0
1 2 989898.0 NaN
Решения для нескольких столбцов и необходимо обрабатывать их одинаково:
print (df)
ID Phone Number Name Val
0 1 234444 A 10
1 1 989898 B 4
2 2 30909 C 6
g = df.groupby('ID').cumcount().add(1)
df = df.set_index([g, 'ID']).unstack()
df.columns = [f'{a}{b}' for a, b in df.columns]
df = df.rename_axis('ID').reset_index()
print (df)
ID Phone Number1 Phone Number2 Name1 Name2 Val1 Val2
0 1 234444.0 30909.0 A C 10.0 6.0
1 2 989898.0 NaN B NaN 4.0 NaN
Или:
df1 = df.groupby('ID').agg(list)
comb = [pd.DataFrame(df1[x].values.tolist(), index=df1.index) for x in df1.columns]
df = pd.concat(comb, axis=1, keys=df1.columns)
df.columns = [f'{a}{b}' for a, b in df.columns]
df = df.rename_axis('ID').reset_index()
print (df)
ID Phone Number0 Phone Number1 Name0 Name1 Val0 Val1
0 1 234444 989898.0 A B 10 4.0
1 2 30909 NaN C None 6 NaN