-1

In my csv in Currency column I am trying to print indexes where Currency = 'Swiss Franc'. My code prints all indexes somehow. This is my code:

import pandas as pd
df = pd.read_csv('data/data.csv', sep=',')
def invalid_currency(df):
    ids_invalid_currency = df[df['Currency'] == 'Swiss Franc'].index.tolist()
    print (ids_invalid_currency)
    return ids_invalid_currency

output_1 = ids_invalid_currency(df)

I tried other function:


def invalid_currency_names(df):
    valid_names = ['USD', 'JKF', 'CAD']
    sus_name = df[~df['Currency'].isin(valid_names)]
    ids_sus_name = sus_name.index.to_list()
    print("Currency with invalid values: {}".format(ids_sus_name))
    return ids_sus_name
out_2 = invalid_currency_names(df)

it also prints every row in a csv

madgoat
  • 1
  • 1
  • Please provide a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) (also look [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) that replicates your problem. Otherwise the chances of getting a meaningful answer are mininmal. – Timus Jun 19 '23 at 08:56

3 Answers3

0

For example 1, perhaps you did not call the function properly. Instead of typing output_1 = invalid_currency(df), you typed output_1 = ids_invalid_currency.

John U.
  • 1
  • 3
  • oh yes I did it, but it still prints all of the rows – madgoat Jun 17 '23 at 07:46
  • Then why is that not present in your code snippet? Please check to make sure your data is not incorrect. Make sure the column name and currency values are right and that your data has properly been delimited. – John U. Jun 17 '23 at 14:35
0

you can use get_group

currency = df.groupby('Currency')
ids_invalid_currency = currency.get_group('Swiss Franc')
zhiguang
  • 345
  • 1
  • 7
0

First of all you have not called your function. try this:

output1 = invalid_currency(df)

then :

rows : df[df['Currency'] == 'Swiss Franc']

should work for you

Ati Srv
  • 36
  • 4