0

I've got dataframe like this original data

and I hope to have new dataframe like below new data

How can I create code for this modification? It need to consolidate first series data and create new dataframe.

forward 24
  • 51
  • 1
  • 1
  • 7
  • original: 1 A2C02158300 2 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD 3 D201,D206,D218,D219,D222,D302,D308,D408, 4 D409,D501,D502,D505,D506,D507,D508 5 A2C02250500 6 T BIP/PUMD3,SOT363,SMD SOLDERING 7 T209,T501,T502 – forward 24 Sep 21 '20 at 23:36
  • new: 1 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D201 2 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D206 3 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D218 4 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D219 5 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D222 6 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D302 7 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D308 8 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D408 9 A2C02158300 D REC/BAS16-03W,100V,250mA,SOD323,0s,SMD D409 – forward 24 Sep 21 '20 at 23:37
  • 1
    can you put the details into the question instead of posting them in the comments section. – Joe Ferndz Sep 21 '20 at 23:39
  • Please check out: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – David Erickson Sep 22 '20 at 00:03

1 Answers1

0

Some imports:

import pandas as pd 
import numpy as np

Here we create dataframe from data you provided:

df = pd.DataFrame({
    "a" : [
           'A2C02158300', 'D REC/BAS16-03W 100V 250mA SOD323 0s SMD', 'D201,D206,D218,D219,D222,D302,D308,D408', 'D409,D501,D502,D505,D506,D507,D508', 
           'A2C02250500', 'T BIP/PUMD3,SOT363,SMD SOLDERING', 'T209,T501,T502'
          ]
})

df.head(10)

Output:

Output

Then we prepare dataframe with first 2 columns:

s1 = df.iloc[::4, :]
s1.reset_index(drop=True, inplace=True)

s2 = df.iloc[1::4, :]
s2.reset_index(drop=True, inplace=True)

df = pd.DataFrame({
    'a': s1['a'],
    'b': s2['a']
})

After that prepare and add third column:

s3 = df.iloc[2::4, :]
s3.reset_index(drop=True, inplace=True)
s3 = s3['a'].str.split(',').apply(pd.Series, 1).stack()
s3.index = s3.index.droplevel(-1)
s3.name = 'c'

df = df.join(s3)
df.reset_index(drop=True, inplace=True)
df

Output:

Output

Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27