Given data
df = pd.DataFrame(
{
'c': ['p1', 'p2', 'p3'],
'v': [ 2 , 8 , 3],
}
)
This outputs
c v
0 p1 2
1 p2 8
2 p3 3
I'm wondering how to create the following using pandas
c v p1 p2 p3
0 p1 2 2 0 0
1 p2 8 0 8 0
2 p3 3 0 0 3
In such a way that I could scale this up to 1000 rows rather than 3 rows (so no hard coding)
edit
my current approach is as follows :
df = pd.DataFrame(
{
'c': ['p1', 'p2', 'p3'],
'v': [ 2 , 8 , 3],
}
)
# create columns with zero
for p in df['c']:
df[p] = 0
# iterate over columns, set values
for p in df['c']:
# get value
value = df.loc[ df.loc[:,'c']==p, 'v']
# get the location of the element to set
idx=df.loc[:,'c']==p
df.loc[idx,p]=value
which outputs the correct result, I feel as though it's a very clunky approach though.
Edit two
The solution must work for the following data :
df = pd.DataFrame(
{
'c': ['p1', 'p2', 'p3', 'p1'],
'v': [ 2 , 8 , 3, 4],
}
)
returning
c v p1 p2 p3
0 p1 2 2 0 0
1 p2 8 0 8 0
2 p3 3 0 0 3
3 p1 9 9 0 0
Meaning that the approach of using a pivot table as
piv = df.pivot_table(index='c', columns='c', values='v', fill_value=0)
df = df.join(piv.reset_index(drop=True))
wouldn't work, although for the original data set it was fine.