Home > Back-end >  how can i exclude the index from my output when using pandas df.loc?
how can i exclude the index from my output when using pandas df.loc?

Time:08-05

From a pandas df i'm trying to output the respective description name, based on the style code (UBR-5912). It outputs the description but it always outputs the index number as well (8338). How can i exclude the index in the output?

I'm using this output later on and it only takes on one output. Thanks.

df.loc[df['STYLE_NO'] == 'UBR-5912', 'DESCRIPTION'] 

output:

8338    PADDED U/WIRED BRA
Name: DESCRIPTION, dtype: object

desired output

PADDED U/WIRED BRA
Name: DESCRIPTION, dtype: object

CodePudding user response:

Another option:

print(df.loc[df['STYLE_NO'] == 'UBR-5912', 'DESCRIPTION'][0])

CodePudding user response:

You may use

print(df.to_string(index=False))

In your case this would be

df.loc[df['STYLE_NO'] == 'UBR-5912', 'DESCRIPTION'].to_string(index=False)

You can also use tabulate

from tabulate import tabulate
print(tabulate(df, headers='keys', tablefmt='plain', showindex=False))

Checkout tabulate, it can print your table in multiple pretty formats

  • Related