Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following dataframe:

+-------------------------------------------+----------------------------------------+----------------+----------------------------------+
|                  Lookup                   |             LookUp Value 1             | LookUp Value 2 |          LookUp Value 3          |
+-------------------------------------------+----------------------------------------+----------------+----------------------------------+
| 300000,50000,500000,100000,1000000,200000 | -1820,-1820,-1820,-1820,-1820,-1820    |    1,1,1,1,1,1 |    1820,1820,1820,1820,1820,1820 |
| 100000,1000000,200000,300000,50000,500000 | -1360,-28760,-1360,-28760,-1360,-28760 |    2,3,2,3,2,3 | 4120,31520,4120,31520,4120,31520 |
+-------------------------------------------+----------------------------------------+----------------+----------------------------------+

Each column is a list, the first columns is the lookup key and the rest are the lookup value. I would like to generate the dataframe like this.

+--------------------+--------------------+--------------------+
| Lookup_300K_Value1 | Lookup_300K_Value2 | Lookup_300K_Value3 |
+--------------------+--------------------+--------------------+
|              -1820 |                  1 |               1820 |
|             -28760 |                  3 |              31520 |
+--------------------+--------------------+--------------------+

Actually I have a solution using pandas.apply and process row by row. It is very very slow so I would like to see if there are some solution that could speed up the process? Thank you very much.

EDIT: I added the dataframe generation code below

d = {'Lookup_Key': ['300000,50000,500000,100000,1000000,200000', '100000,1000000,200000,300000,50000,500000'],
     'LookUp_Value_1': ['-1820,-1820,-1820,-1820,-1820,-1820', '-1360,-28760,-1360,-28760,-1360,-28760'],
     'LookUp_Value_2': ['1,1,1,1,1,1', '2,3,2,3,2,3'],
     'LookUp_Value_3': ['1820,1820,1820,1820,1820,1820', '4120,31520,4120,31520,4120,31520']}
df = pd.DataFrame(data=d)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

Solution tested with missing values in some column(s), but in Lookup are not NaNs or Nones:

df = pd.concat([df[x].str.split(',', expand=True).stack() for x in df.columns], axis=1, keys=df.columns)
df = df.reset_index(level=1, drop=True).set_index('Lookup', append=True).unstack().sort_index(axis=1, level=1)
df.columns = [f'{b}_{a}' for a, b in df.columns]

Idea is split each value in loop, explode for Series and concat together, last reshape by stack:

df = pd.concat([df[x].str.split(',').explode() for x in df.columns], axis=1)
df = df.set_index('Lookup', append=True).unstack().sort_index(axis=1, level=1)
df.columns = [f'{b}_{a}' for a, b in df.columns]
print (df)
  100000_LookUp Value 1 100000_LookUp Value 2 100000_LookUp Value 3  
0                 -1820                     1                  1820   
1                 -1360                     2                  4120   

  1000000_LookUp Value 1 1000000_LookUp Value 2 1000000_LookUp Value 3  
0                  -1820                      1                   1820   
1                 -28760                      3                  31520   

  200000_LookUp Value 1 200000_LookUp Value 2 200000_LookUp Value 3  
0                 -1820                     1                  1820   
1                 -1360                     2                  4120   

  300000_LookUp Value 1 300000_LookUp Value 2 300000_LookUp Value 3  
0                 -1820                     1                  1820   
1                -28760                     3                 31520   

  50000_LookUp Value 1 50000_LookUp Value 2 50000_LookUp Value 3  
0                -1820                    1                 1820   
1                -1360                    2                 4120   

  500000_LookUp Value 1 500000_LookUp Value 2 500000_LookUp Value 3  
0                 -1820                     1                  1820  
1                -28760                     3                 31520  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...