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
You can replace values to strings, then empty strings to
NaN, so if add columns names getNaNs withDataFrame.raddfor add from right side, last only replaceNaNs to empty strings: