Re: help: pandas and 2d table

2024-04-13 Thread Tim Williams via Python-list
On Sat, Apr 13, 2024 at 1:10 PM Mats Wichmann via Python-list <
python-list@python.org> wrote:

> On 4/13/24 07:00, jak via Python-list wrote:
>
> doesn't Pandas have a "where" method that can do this kind of thing? Or
> doesn't it match what you are looking for?  Pretty sure numpy does, but
> that's a lot to bring in if you don't need the rest of numpy.
>
> pandas.DataFrame.where — pandas 2.2.2 documentation (pydata.org)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help: pandas and 2d table

2024-04-13 Thread Mats Wichmann via Python-list

On 4/13/24 07:00, jak via Python-list wrote:

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.




doesn't Pandas have a "where" method that can do this kind of thing? Or 
doesn't it match what you are looking for?  Pretty sure numpy does, but 
that's a lot to bring in if you don't need the rest of numpy.



--
https://mail.python.org/mailman/listinfo/python-list


Re: help: pandas and 2d table

2024-04-13 Thread jak via Python-list

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.


--
https://mail.python.org/mailman/listinfo/python-list


Re: How to Add ANSI Color to User Response

2024-04-13 Thread Pierre Fortin via Python-list
On Thu, 11 Apr 2024 05:00:32 +0200 Gisle Vanem via Python-list wrote:

>Pierre Fortin wrote:
>
>> Over the years, I've tried different mechanisms for applying colors until
>> I got my hands on f-stings; then I created a tiny module with all the
>> colors (cR, cG, etc) which made my life so much simpler (attached).  
>
>Attachments are stripped off in this list.
>It would be nice to see this tiny module of yours.
>An URL or attach as inline text please.

#!/bin/python
# -*- mode: python; -*-
# Copyright:
#2024-Present, Pierre Fortin 
# License:
#GPLv3 or any later version: https://www.gnu.org/licenses/gpl-3.0.en.html
# Created:
#2023-11-10 Initial script
# Updated: 

# Usage:  f"{cR}red text {cG}green text{cO}; colors off"
#or:  print( cY, "yellow text", cO )

# VT100 type terminal colors
ESC = "\u001b";
# Foreground Colors
_black = f"{ESC}[30m"; _red = f"{ESC}[31m"; _green = f"{ESC}[32m"; _yellow = 
f"{ESC}[33m"
_blue = f"{ESC}[34m"; _magenta = f"{ESC}[35m"; _cyan = f"{ESC}[36m"; _white = 
f"{ESC}[37m"
# Background Colors
_black_ = f"{ESC}[40m"; _red_ = f"{ESC}[41m"; _green_ = f"{ESC}[42m"; _yellow_ 
= f"{ESC}[43m"
_blue_ = f"{ESC}[44m"; _magenta_ = f"{ESC}[45m"; _cyan_ = f"{ESC}[46m"; _white_ 
= f"{ESC}[47m"

_off = f"{ESC}[0m"
ANSIEraseLine = '\033[2K\033[1G'
EL = ANSIEraseLine # short alias

# Color abbreviations (shortcuts for f-sting use)
cK=_black; cR=_red; cG=_green; cY=_yellow; cB=_blue; cM=_magenta; cC=_cyan; 
cW=_white; cO=_off
# background colors; use {cO} to turn off any color
bK=_black_; bR=_red_; bG=_green_; bY=_yellow_; bB=_blue_; bM=_magenta_; 
bC=_cyan_; bW=_white_
-- 
https://mail.python.org/mailman/listinfo/python-list