Re: help: pandas and 2d table

Liste des GroupesRevenir à cl python 
Sujet : Re: help: pandas and 2d table
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 13. Apr 2024, 20:39:51
Autres entêtes
Organisation : Stefan Ram
Message-ID : <pandas-20240413193824@ram.dialup.fu-berlin.de>
References : 1 2 3 4 5 6
Tim Williams <tjandacw@gmail.com> wrote or quoted:
e.where.html#pandas.DataFrame.where>

  Threw together a quick thing with "where", see how it looks.

import pandas as pd

# Warning! Will overwrite the file 'file_20240412201813_tmp_DML.csv'!
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 )

# Note the "index_col=0" below, which is important here!
df = pd.read_csv( 'file_20240412201813_tmp_DML.csv', index_col=0 )

df = df.where( df == 'zz' ).stack().reset_index()
result ={ 'zz': list( zip( df.iloc[ :, 0 ], df.iloc[ :, 1 ]))}

print( result )

  Prints here:

{'zz': [('foo1', 'foo3'), ('foo2', 'foo5'), ('foo3', 'foo2'), ('foo3', 'foo6'), ('foo5', 'foo4')]}

  .

Date Sujet#  Auteur
12 Apr 24 * help: pandas and 2d table10jak
12 Apr 24 `* Re: help: pandas and 2d table9Stefan Ram
13 Apr 24  `* Re: help: pandas and 2d table8jak
13 Apr 24   +- Re: help: pandas and 2d table1Mats Wichmann
13 Apr 24   `* Re: help: pandas and 2d table6Tim Williams
13 Apr 24    `* Re: help: pandas and 2d table5Stefan Ram
13 Apr 24     `* Re: help: pandas and 2d table4jak
14 Apr 24      `* Re: help: pandas and 2d table3Stefan Ram
15 Apr 24       +- Re: help: pandas and 2d table1jak
19 May 24       `- Re: help: pandas and 2d table1Stefan Ram

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal