How to bring the csv data into spacy v2.0 version

Viewed 23

Currently I have the csv data in below format:

enter image description here

However I want the data to be in spacy (version 2.0) format, where it should be grouped by "data" column in below format:

enter image description here

I have tried it to group by "data" field using code below:

def data_format():
    df = pd.read_csv("Training_data_sample.csv")
    grouped = df.groupby(['data'])
    df_1 = pd.DataFrame(grouped)
    df_1.to_csv("Test_format.csv", encoding="utf-8",index=False)
1 Answers

try:

df
    data    positions   field
0   data1   (37,41)     fielf_1
1   data1   (4,14)      fielf_2
2   data1   (88,121)    fielf_3
3   data1   (227,267)   fielf_4
4   data2   (37,41)     fielf_1
5   data2   (4,14)      fielf_2
6   data2   (88,121)    fielf_3
7   data2   (227,267)   fielf_4

df['n'] = df.apply(lambda x: (x['positions'], x['field']), axis=1)
df.groupby('data')['n'].apply(list).reset_index()

    data    n
0   data1   [((37,41), fielf_1), ((4,14), fielf_2), ((88,1...
1   data2   [((37,41), fielf_1), ((4,14), fielf_2), ((88,1...

#if you need the result in dictionary format:
df.groupby('data')['n'].apply(list).to_dict()

{'data1': [('(37,41)', 'fielf_1'), ('(4,14)', 'fielf_2'), ('(88,121)', 'fielf_3'), ('(227,267)', 'fielf_4')],
 'data2': [('(37,41)', 'fielf_1'), ('(4,14)', 'fielf_2'), ('(88,121)', 'fielf_3'), ('(227,267)', 'fielf_4')]}

Related