如何将一个列表添加到另一个列表?熊猫df.loc



我是熊猫新手。

我的df是一个包含会计信息的CSV,我有一个包含帐户列表的变量,我想先将它们添加到另一个变量中,然后使用df.loc


df = pd.read_csv('2021BALANCE.csv')
menor = (df['account_1'] < 10000)  # just filtering 

#LIST OF ACCOUNTS inside variables 

assets = ['account_1', 'account_2', 'account_3']
liabilities = ['account_6' , 'account_4']
profits = ['account_20' , 'account_21']
#THIS VARIABLE already have accounts, and I want to add the accounts above to it.
all_accounts = ['account_9345', 'account_234623' ,assets, liabilities, profits]

df.loc[menor, all_accounts]

TypeError: unhashable type: 'list'

有很多变量里面都有帐户,为了简化,我把3放在这里,我想把它们都放在一个变量all_accounts所以我可以使用df。loc [menor all_accounts].

非常感谢你的帮助!

如果你想"合并"将列表转换为单个扁平列表时,需要使用展开操作符(https://how.wtf/spread-operator-in-python.html)来"解包"。你要插入到新表中的列表。否则,您将嵌套列表,导致.loc抛出错误。

尝试在声明"all_accounts":

all_accounts = ['account_9345', 'account_234623' ,*assets, *liabilities, *profits]

最新更新