0

I have an issue with keeping some data from duplicates and wanting to add valuable information to a new column in the dataframe.

import pandas as pd
data = {'id':[1,2,2,3],'company':[1,2,2,1],'bank':['a', 'x', 'y', 'a'], 
    'round': ['seed', 'seed', 'seed', 'series a'], 'funding': [100, 200, 200, 300],
   'date': ['2006-12-01', '2004-09-01', '2004-09-01', '2007-05-01']}
df = pd.DataFrame(data, columns = ['id','company', 'round', 'bank', 'funding', 'date'])
print df

Yields:

   id  company     round   bank    funding        date
0   1        1      seed      a        100  2006-12-01
1   2        2      seed      x        200  2004-09-01
2   2        2      seed      y        200  2004-09-01
3   3        1  series a      a        300  2007-05-01

Desired Output:

     company   round_0     bank_0   funding_0      date_0    round_1    bank_1  funding_1      date_1 
0          1      seed          a         100  2006-12-01   series a         a        300  2007-05-01
1          2      seed     [x, y]         200  2004-09-01       None      None       None        None

I'm thinking that a pivot/melt might work? Along with a groupby('company', 'round')?

Also, I'm open to the idea of changing the dtype for the new bank column list (ie dictionary). And, the integers could be replaced with the "round" column info. Making the columns:

company, bank_seed, funding_seed, date_seed, bank_series_a, funding_series_a, date_series_a
7
  • Can you explain the logic for bank_0_1, label_1, bank_1_0, funding_1 and date_1? Why are some of them Nones?
    – Allen Qin
    Commented May 10, 2017 at 23:13
  • I'm using 0 indexing, which might be causing some confusion. bank_0_0 is the first bank of the first round. bank_1_0 is the first bank of the second round. So the first number corresponds to the relative label. Therefore, there are cases where there isn't a second bank who has funded that particular company for that particular label. Does that help? Commented May 10, 2017 at 23:25
  • Is the number of round always 2? number of bank always 2? or are they variable?
    – Allen Qin
    Commented May 10, 2017 at 23:53
  • @Allen Thank you for following up! The number of rounds could be many. As could be the number of banks. Commented May 11, 2017 at 0:00
  • This means the number of columns of your df will vary. Not quite straightforward.
    – Allen Qin
    Commented May 11, 2017 at 0:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.