Home>

I have several folders from which I loop through the files, create a name and count column in them The name column in each file consists of a portion of the values ​​from the name column in another file read into df. Name values ​​are different in all files. I need to add a count column from each file to df based on these values.

For example, I have two tables like this:

id name count
a 48 /a 2
c 48 /c 5
id name count
a 50 /a 5
b 50 /b 6
d 50 /d 6

You need to get such a table by adding from the previous two count values ​​to the original df table

name count
48 /a 2
48 /b
48 /c 5
50 /a 5
50 /b 6
50 /c
50 /d 6

Code

for i in range (len (file)):
    obj= pd.read_csv (file [i])
    obj ['count']= obj.groupby ('id') ['id']. transform ('count')
    for j in range (obj.shape [0]):
        obj ['name'] [j]= str (file [i]) + '/' + str (obj ['id'] [j])
    obj.to_csv ('frame' + str (i) + '. csv')
    df= df.merge (obj [['name', 'count']], on= 'link', how= 'outer')

But my code gives me a table like this:

name count_x count_y
48 /a 2
48 /b
48 /c 5
50 /a 5
50 /b 6
50 /c
50 /d 6

How can you rewrite everything to be added to one column?