Use python3isin function but cannot extract data in order

Asked 2 years ago, Updated 2 years ago, 46 views

The following files are available:
Df12 Original File
url, url_category, todo
A,app,0
B,app,1
B,app,2
A,app,3
C,matome,4
........
In the above form, todo is not duplicated from 1 to 100, and other items are duplicated.

final_result= 
[[9, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
 [40, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

If you run the isin function below, the order will change.
How can it be kept in the same order as above?For example, 9-0-1-2, 40-41-0

Run to

cnt=0
for final_result:
    cnt+=1
    print(df12[df12['todo'].isin(l)])

The result will be as follows and the order of todo will be changed.
url_category, todo
app,0
app,0
app,1
app,2
matome,9

Originally, I would like to arrange it according to the list above.The following is the correct answer.
url_category, todo
matome,9
app,0
app,1
app,2
app,0

python3 pandas

2022-09-30 15:44

2 Answers

You can also list the indexes in the todo column that match the criteria and retrieve them with iloc().

import pandas as pd

df = pd.DataFrame({
  'url': ['A', 'B', 'B', 'A', 'C',
  'url_category': ['app', 'app', 'app', 'app', 'matome'],
  'todo': [0, 1, 2, 3, 9],
})
print(df)

final_result=[
  [9, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]

dfs=[[]]*len(final_result)
for nth, line enumerate (final_result):
  dfs[nth] = df.iloc [[df[df['todo'] == i].index[0]for i l]]\
               .reset_index(drop=True)

print(dfs[0])

## df
  url url_category todo
0A app 0
1Bapp1
2Bapp2
3App3
4Cmatome9

## Sort by final_results
   url url_category todo
0Cmatome9
1App0
2Bapp1
3Bapp2
4A app0
5A app0
6A app 0
7A app0
8A app0
9A app 0
10A app 0
11A app0
12A app0
13A app0

add

Isn't there a way to take the second line?

I think it will be as follows.However, for the last line of the data frame, there is no second line, so it remains the same.

 for nth, line enumerate (final_result):
  lst=sum([
    [j,j+1] if j<len(df)-1 else[j]
    for jin [df[df['todo']==i].index[0]for iinl]],[])
  dfs[nth] = df.iloc[lst].reset_index(drop=True)

print(dfs[0])

   url url_category todo
0Cmatome9
1App0
2Bapp1
3Bapp1
4Bapp2
5Bapp2
6App3
7A app0
8Bapp1
9A app 0
10B app1
           :
           :


2022-09-30 15:44

I think it will look like this.

for final_result:
    wdf = pd.DataFrame()
    for x in l:
        wdf=wdf.append (df12[df12['todo'].isin([x]], ignore_index=True)

    print(wdf)


2022-09-30 15:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.