Add column and row index values to Frame values

User 5141 | 4/22/2016, 5:01:01 PM

Is it possible to add the row and column index numbers to the values within a Frame?

Current format:

0 | 1 | 2 | 3 | 
1 val val val
2 val val val

Expected output:

1:1:val 1:2:val 1:3:val
2:1:val 2:2:val 2:3:val

Thanks

Comments

User 1174 | 4/22/2016, 6:17:52 PM

Hi,

There is a method within SFrame to add a new column containing the row index: addrownumber.

you can simply do: sf = sf.add_row_number()

As for column index, it would harder.

What is your use case to include both row and column index in the value?


User 5141 | 4/22/2016, 7:01:50 PM

I am looking to use LIBFFM which is a library for field-aware factorisation machine.

The data format of LIBFFM is:

label field1:index1:value1 field2:index2:value2

http://www.csie.ntu.edu.tw/%7Er01922136/libffm/

Any help is greatly appreciated.


User 19 | 4/22/2016, 9:39:04 PM

Hi archienorman_io,

Try this:

sf = gl.SFrame({'0': ['a', 'b', 'c'], '1': ['d', 'e', 'f'], '2': ['h', 'i', 'j']}) sf = sf.add_row_number() def transform_row(row): return [':'.join([str(row['id']), str(k), v]) for k, v in row.items() if k != 'id'] sf['answer'] = sf.apply(lambda row: ' '.join(sorted(transform_row(row)))) +----+---+---+---+-------------------+ | id | 0 | 1 | 2 | answer | +----+---+---+---+-------------------+ | 0 | a | d | h | 0:0:a 0:1:d 0:2:h | | 1 | b | e | i | 1:0:b 1:1:e 1:2:i | | 2 | c | f | j | 2:0:c 2:1:f 2:2:j | +----+---+---+---+-------------------+

Let us know if that helps!

Chris


User 5141 | 4/23/2016, 1:25:48 AM

Hi Chris,

This is great - I will try it with your convert.py script for the libffm wrapper.

Thanks for the help! Much appreciated.