Sujet : Re: help: pandas and 2d table
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 12. Apr 2024, 20:22:54
Autres entêtes
Organisation : Stefan Ram
Message-ID : <pandas-20240412202220@ram.dialup.fu-berlin.de>
References : 1
jak <
nospam@please.ty> 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 )