Home > Enterprise >  Copy column names into rows but skip it when there's no value
Copy column names into rows but skip it when there's no value

Time:11-16

I want to send a list of column names into their rows but skip it when there's no value. What's the right way to achieve this?

data = {'fruit_tag': {0: 'apple', 1: 'apple', 2: 'banana', 3: 'apple', 4: 'watermelon'}, 'location': {0: 'Hong Kong', 1: 'Tokyo', 2: '', 3: '', 4: ''}, 'rating': {0: 'bad', 1: 'good', 2: 'good', 3: 'bad', 4: 'good'}, 'measure_score': {0: 0.9529434442520142, 1: 0.952498733997345, 2: 0.9080725312232971, 3: 0.8847543001174927, 4: 0.8679852485656738}}
dat = pd.DataFrame.from_dict(data)

    fruit_tag   location rating  measure_score
0       apple  Hong Kong    bad       0.952943
1       apple      Tokyo   good       0.952499
2      banana              good       0.908073
3       apple               bad       0.884754
4  watermelon              good       0.867985

Expected output

    fruit_tag             location        rating                      measure_score
0       apple  location: Hong Kong   rating: bad  measure_score: 0.9529434442520142
1       apple      location: Tokyo  rating: good   measure_score: 0.952498733997345
2      banana                       rating: good  measure_score: 0.9080725312232971
3       apple                        rating: bad  measure_score: 0.8847543001174927
4  watermelon                       rating: good  measure_score: 0.8679852485656738

CodePudding user response:

You can replace values to strings, then empty strings to NaN, so if add columns names get NaNs with DataFrame.radd for add from right side, last only replace NaNs to empty strings:

dat.iloc[:, 1:] = dat.iloc[:, 1:].astype(str).replace('', np.nan).radd(dat.columns[1:]   ': ').fillna('')
print (dat)
    fruit_tag             location        rating  \
0       apple  location: Hong Kong   rating: bad   
1       apple      location: Tokyo  rating: good   
2      banana                       rating: good   
3       apple                        rating: bad   
4  watermelon                       rating: good   

                       measure_score  
0  measure_score: 0.9529434442520142  
1   measure_score: 0.952498733997345  
2  measure_score: 0.9080725312232971  
3  measure_score: 0.8847543001174927  
4  measure_score: 0.8679852485656738  
  • Related