Home > database >  function to sort rows
function to sort rows

Time:06-09

I have the following code which filters a row based on the part number.

df= sets[sets['num_parts'] == 11695]
df

enter image description here

Now, i want to write a function where i can simply call a function and pass set_num as a string and return the rows.

Here is what I have tried. This is giving me an error.

def select_set_row(df):
    return display(df.iloc['set_num'])

CodePudding user response:

iloc is integer-location based indexing. You should use df['set_num'].iloc[0]

CodePudding user response:

This is exactly not a function, but you can create similar function.

Sample data:

df = pd.DataFrame({'id': [1,2,3,4,5,6], 'num_parts': [132, 234, 345, 578, 462, 222]})

print(df)

list of num_parts:

num_parts = df['num_parts'].tolist()

Outputing dataframe for each num_parts:

for parts in num_parts:
    print(df[df['num_parts'] == parts])
  • Related