csv pandas不显示列中的特定字符串值,它显示所有字符串值,

yebdmbv4  于 2023-06-19  发布在  其他
关注(0)|答案(3)|浏览(101)

在我的csv in Currency列中,我试图打印Currency = 'Swiss Franc'的索引。我的代码以某种方式打印所有索引。这是我的代码:

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)

我尝试了其他功能:

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)

它还打印csv中的每一行

hjzp0vay

hjzp0vay1#

例如1,您可能没有正确调用该函数。您没有键入output_1 = invalid_currency(df),而是键入output_1 = ids_invalid_currency

1wnzp6jl

1wnzp6jl2#

可以使用get_group

currency = df.groupby('Currency')
ids_invalid_currency = currency.get_group('Swiss Franc')
xtfmy6hx

xtfmy6hx3#

首先,你没有调用你的函数。试试这个:

output1 = invalid_currency(df)

然后:

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

应该对你有用

相关问题