I have a dataframe that has names of an item, data on it, and then competitor data all in one row:
name value1 value2 ex_value1 ex_value2
jim 0.4 0.6 0.7 0.3
tim 0.2 0.8 0.766666667 0.233333333
john 1 0 0.5 0.5
paul 0.9 0.1 0.533333333 0.466666667
What I want to do is create a new table that has indexes by name, but inserts new rows based on the competitor data, so that it shows jim, ex-jim, tim,ex-tim, etc:
name value1 value2
jim 0.4 0.6
tim 0.2 0.8
john 1 0
paul 0.9 0.1
ex_jim 0.7 0.3
ex_tim 0.766666667 0.233333333
ex_john 0.5 0.5
ex_paul 0.533333333 0.466666667
How would I go about doing this? Would I have to set index on name, then insert new that way? Would I got about this through a loop? Appreciate guidance on this
You can do this using concat
df_ex = df[['name','ex_value1', 'ex_value2']].rename(columns = {'ex_value1': 'value1', 'ex_value2': 'value2'})
df_ex['name'] = 'ex_' + df_ex['name']
pd.concat([df[['name','value1', 'value2']], df_ex ]).round(2)
name value1 value2
0 jim 0.40 0.60
1 tim 0.20 0.80
2 john 1.00 0.00
3 paul 0.90 0.10
0 ex_jim 0.70 0.30
1 ex_tim 0.77 0.23
2 ex_john 0.50 0.50
3 ex_paul 0.53 0.47
I would like recreate the df, you can add the reset_index()
at the end
pd.DataFrame(df.iloc[:,1:].values.reshape(8,2),index=['','ex_']*4+df.name.repeat(2),columns=['value1','value2'])
Out[986]:
value1 value2
name
jim 0.400000 0.600000
ex_jim 0.700000 0.300000
tim 0.200000 0.800000
ex_tim 0.766667 0.233333
john 1.000000 0.000000
ex_john 0.500000 0.500000
paul 0.900000 0.100000
ex_paul 0.533333 0.466667
I would recommend splitting your dataframe into two and then concatting it back together. Something like:
import pandas as pd
df = pd.DataFrame([['jim', .4, .6, .7, .3], ['john', 1, 0, .5, .5]], columns=['name', 'value1', 'value2', 'ex_value1', 'ex_value2'])
ex_df = df.copy()
ex_df['name'] = 'ex_'+ex_df['name'].astype(str)
ex_df = ex_df[['name', 'ex_value1', 'ex_value2']]
ex_df.columns = ['name', 'value1', 'value2']
df = df[['name', 'value1', 'value2']]
frames = (df, ex_df)
new = pd.concat(frames).reset_index()
new = new[['name', 'value1', 'value2']]
print(new)
#output
name value1 value2
0 jim 0.4 0.6
1 john 1.0 0.0
2 ex_jim 0.7 0.3
3 ex_john 0.5 0.5
You could go for
def myfunc(row):
return pd.Series({'name': 'ex_{}'.format(row['name']),
'value1': row['ex_value1'],
'value2': row['ex_value2']})
df2 = df[~df['name'].astype(str).str.startswith('ex_')].apply(myfunc,axis =1)
df = pd.concat([df[['name', 'value1', 'value2']], df2])
This applies the function myfunc
only to those rows where name
does not start with ex_
. myfunc()
returns a new dataframe which is then concatenated to df
.
df = pd.concat([df[['name', 'value1', 'value2']], df[~df['name'].astype(str).str.startswith('ex_')].apply(myfunc,axis = 1)])
You could use a combination of melt
and pivot
df2 = df.melt('name')
df2.loc[df2.variable.str.contains('ex'),'name'] = 'ex_' +df2.name
df2.variable = df2.variable.str.strip('ex_')
df2 = df2.pivot(index='name',columns='variable').reset_index()
df2.columns = df2.columns.droplevel(0)
which gives you
variable value1 value2
0 ex_jim 0.700000 0.300000
1 ex_john 0.500000 0.500000
2 ex_paul 0.533333 0.466667
3 ex_tim 0.766667 0.233333
4 jim 0.400000 0.600000
5 john 1.000000 0.000000
6 paul 0.900000 0.100000
7 tim 0.200000 0.800000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.