Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: jak Newsgroups: comp.lang.python Subject: Re: help: pandas and 2d table Date: Sat, 13 Apr 2024 15:00:35 +0200 Organization: A noiseless patient Spider Lines: 50 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 13 Apr 2024 15:00:35 +0200 (CEST) Injection-Info: dont-email.me; posting-host="85eb45bab8bf5c02392665bafaa869d9"; logging-data="3175194"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fOGygLc4Xa+kyFrIaiU0J" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.18.2 Cancel-Lock: sha1:OR8+pX8WCwcJR36ov4SM6SLm6eA= In-Reply-To: Bytes: 2551 Stefan Ram ha scritto: > jak wrote or quoted: >> Would you show me the path, please? > > I was not able to read xls here, so I used csv instead; Warning: > the script will overwrite file "file_20240412201813_tmp_DML.csv"! > > import pandas as pd > > with open( 'file_20240412201813_tmp_DML.csv', 'w' )as out: > print( '''obj,foo1,foo2,foo3,foo4,foo5,foo6 > foo1,aa,ab,zz,ad,ae,af > foo2,ba,bb,bc,bd,zz,bf > foo3,ca,zz,cc,cd,ce,zz > foo4,da,db,dc,dd,de,df > foo5,ea,eb,ec,zz,ee,ef > foo6,fa,fb,fc,fd,fe,ff''', file=out ) > > df = pd.read_csv( 'file_20240412201813_tmp_DML.csv' ) > > result = {} > > for rownum, row in df.iterrows(): > iterator = row.items() > _, rowname = next( iterator ) > for colname, value in iterator: > if value not in result: result[ value ]= [] > result[ value ].append( ( rowname, colname )) > > print( result ) > In reality what I wanted to achieve was this: what = 'zz' result = {what: []} for rownum, row in df.iterrows(): iterator = row.items() _, rowname = next(iterator) for colname, value in iterator: if value == what: result[what] += [(rowname, colname)] print(result) In any case, thank you again for pointing me in the right direction. I had lost myself looking for a pandas method that would do this in a single shot or almost.