winnie
winnie

Reputation: 135

How to combine some rows into a single row

Sorry, I should delete the old question, and create the new one. I have a dataframe with two columns. The df looks as follows:

                     Word   Tag
0                    Asam   O
1               instruksi   O
2                       -   O
3               instruksi   X
4                  bahasa   Y
5               Instruksi   P
6                       -   O
7               instruksi   O
8                  sebuah   Q
9                  satuan   K
10                      -   L
11                 satuan   O
12                   meja   W
13                   Tiap   Q
14                      -   O
15                   tiap   O
16               karakter   P
17                      -   O
18                     ke   O
19                      -   O
20               karakter   O

and I'd like to merge some rows which contain dash - to one row. so the output should be the following:

                     Word   Tag
0                    Asam   O
1     instruksi-instruksi   O
2                  bahasa   Y
3     Instruksi-instruksi   P
4                  sebuah   Q
5           satuan-satuan   K
6                    meja   W
7               Tiap-tiap   Q
8    karakter-ke-karakter   P

Any ideas? Thanks in advance. I have tried the answer from Jacob K, it works, then I found in my dataset, there are more than one - row in between. I have put the expected output, like index number 8

Solution from Jacob K:

# Import packages
import pandas as pd
import numpy as np

# Get 'Word' and 'Tag' columns as numpy arrays (for easy indexing)
words = df.Word.to_numpy()
tags = df.Tag.to_numpy()

# Create empty lists for new colums in output dataframe
newWords = []
newTags = []

# Use while (rather than for loop) since index i can change dynamically
i = 0                             # To not cause any issues with i-1 index
while (i < words.shape[0] - 1):
    if (words[i] == "-"):
        # Concatenate the strings above and below the "-"
        newWords.append(words[i-1] + "-" + words[i+1])
        newTags.append(tags[i-1])
        i += 2                         # Don't repeat any concatenated values
    else:
        if (words[i+1] != "-"):
            # If there is no "-" next, append the regular word and tag values
            newWords.append(words[i])
            newTags.append(tags[i])
        i += 1                         # Increment normally
        
# Create output dataframe output_df        
d2 = {'Word': newWords, 'Tag': newTags}
output_df = pd.DataFrame(data=d2)

Upvotes: 1

Views: 177

Answers (2)

ansev
ansev

Reputation: 30920

My approach with GroupBy.agg:

#df['Word'] = df['Word'].str.replace(' ', '') #if necessary
blocks = df['Word'].shift().ne('-').mul(df['Word'].ne('-')).cumsum()
new_df = df.groupby(blocks, as_index=False).agg({'Word' : ''.join, 'Tag' : 'first'})
print(new_df)

Output

                   Word Tag
0                  Asam   O
1   instruksi-instruksi   O
2                bahasa   Y
3   Instruksi-instruksi   P
4                sebuah   Q
5         satuan-satuan   K
6                  meja   W
7             Tiap-tiap   Q
8  karakter-ke-karakter   P

Blocks (Detail)

print(blocks)
0     1
1     2
2     2
3     2
4     3
5     4
6     4
7     4
8     5
9     6
10    6
11    6
12    7
13    8
14    8
15    8
16    9
17    9
18    9
19    9
20    9
Name: Word, dtype: int64

Upvotes: 1

gtomer
gtomer

Reputation: 6564

This is a loop version:

import pandas as pd
# import data
DF = pd.read_csv("table.csv")
# creates a new DF
newDF = pd.DataFrame()
# iterate through rows
for i in range(len(DF)-1):
    # prepare prev row index (?dealing with private instance of first row)
    prev = i-1
    if (prev < 0):
        prev = 0
    # copy column if the row is not '-' and the next row is not '-'
    if (DF.loc[i+1, 'Word'] != '-'):
        if (DF.loc[i, 'Word'] != '-' and DF.loc[prev, 'Word'] != '-'):
            newDF = newDF.append(DF.loc[i, :])
    # units the three rows if the middle one is '-'
    else:
        row = {'Tag': [DF.loc[i, 'Tag']], 'Word': [DF.loc[i, 'Word']+DF.loc[i+1, 'Word']+DF.loc[i+2, 'Word']]} 
        newDF = newDF.append(pd.DataFrame(row))

Upvotes: 0

Related Questions