lunes, 30 de septiembre de 2024

Pandas cookbook


Read the excel file 

>>> df = pd.read_excel('/home/ambiorixg12/Downloads/500.xlsx')

Save values as list
lst= df.values.tolist()

Count values  from the list 

 len(lst)
500


Print first and last entry
>> print(lst[0],'\n',lst[-1])
['Gaston', 'Willie', 6009.0, nan, 'Howells Ferry', 'Rd', nan, nan, 'Mobile', 'AL', 36618, '(251) 304-1631', 'Male'] 
 ['Paulo', 'Frank', 1801.0, nan, 'Ridgecrest', 'St', nan, nan, 'Valdosta', 'GA', 31601, '(229) 671-9476', 'Male']


Get columns name


headers = df.columns.tolist()

Data type
print(df.dtypes)




-----


Create a dict with the first value and include the column names
God version
 df = pd.read_excel('/home/ambiorixg12/Downloads/500.xlsx')
 c={df.columns.tolist()[a]:df.values.tolist()[0][a] for a in range(len(df.values.tolist()[0])) }


get the first value of are data set
>>> v=[a for a in  df.values.tolist()[0]]  
>>> 
>>> 
>>> 
>>>  ge colum names
>>> h=[a for a in  df.columns.tolist()]


>>>  create are dict
>>> new={h[v.index(a)]:a for a in v}
>>> print(new)
{'Last Name': 'Gaston', 'First Name': 'Willie', 'House Number': 6009.0, 'Pre-directional': nan, 'Street': 'Howells Ferry', 'Street Suffix': 'Rd', 'City': 'Mobile', 'State': 'AL', 'Zip Code': 36618, 'Phone Number': '(251) 304-1631', 'Gender': 'Male'}
>>> 

Or simplify version


>>> new={h[v.index(a)]:a for a in [a for a in  df.values.tolist()[0]]}



---getting header

>>> c={df.columns.tolist()[a] for a in range(len(df.values.tolist()[0])) }
>>> c
{'First Name', 'Gender', 'Pre-directional', 'State', 'House Number', 'Post-directional', 'Last Name', 'Phone Number', 'Street', 'Zip Code', 'City', 'Apartment Number', 'Street Suffix'}
>>> 

info=requests.get('https://ipinfo.io/json').json() 

>>> df = pd.DataFrame.from_dict(info, orient='index', columns=['value'])


Reading files as list and searching

>>> f=open('/tmp/1.txt','r').readlines()

>>> [a.strip() for a in f  if 'pe' in a.lower()]

['Katherine Montolio Perez']






>>> f=open('/tmp/1.txt','r')

>>> f.readlines()

['Ambiorix Rodriguez Placenio\n', 'Ruth Cedeno Lara\n', 'Katherine Montolio Perez\n', 'Armando Rodriguez Rosario\n']



do the magic

>>> f=open('/tmp/1.txt','r')

>>> [a.strip() for a in f.readlines()]

['Ambiorix Rodriguez Placenio', 'Ruth Cedeno Lara', 'Katherine Montolio Perez', 'Armando Rodriguez Rosario']

>>>



>>> 

>>> f=open('/tmp/1.txt','r')

>>> [a.strip().upper() for a in f.readlines()]

['AMBIORIX RODRIGUEZ PLACENIO', 'RUTH CEDENO LARA', 'KATHERINE MONTOLIO PEREZ', 'ARMANDO RODRIGUEZ ROSARIO 


sábado, 28 de septiembre de 2024

if else on lambda

 >>> test = lambda c, a: print(a) if a > c else print(c)

>>> 

>>> test(12,1)

12

>>> test(12,111)

111

>>> 


jueves, 19 de septiembre de 2024

List exercises


Below are a set of scores that students have received in the past semester. Write code to determine how many are 90 or above and assign that result to the value a_scores.


 scores = "67 80 90 78 93 20 79 89 96 97 92 88 79 68 58 90 98 100 79 74 83 88 80 86 85 70 90 100"

a_scores=len([c for c in [ int(a) for a in scores.split()] if c>=90])


Write code that uses the string stored in org and creates an acronym which is assigned to the variable acro. Only the first letter of each word should be used, each letter in the acronym should be a capital letter, and there should be nothing to separate the letters of the acronym. Words that should not be included in the acronym are stored in the list stopwords. For example, if org was assigned the string “hello to world” then the resulting acronym should be “HW”.


stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]

org = "The organization for health, safety, and education"

acro=[a[0].upper() for a in org.split() if a not in stopwords]

acro=''.join(acro)

print(''.join(acro))



-------------

p_phrase = "was it a car or a cat I saw"


# Reverse the original phrase, keeping the spaces and case

r_phrase = p_phrase[::-1]


# Check if the original phrase is equal to the reversed version

is_palindrome = p_phrase.replace(" ", "").lower() == r_phrase.replace(" ", "").lower()


# Print the results

print("Original phrase:", p_phrase)

print("Reversed phrase:", r_phrase)

print("Is palindrome:", is_palindrome)


miércoles, 18 de septiembre de 2024

Python Virtual Environment

Step 1: Install Python

First, ensure you have Python installed. You can check by running:

python3 --version

If it's not installed, you can install it using:
sudo apt update sudo apt install python3 python3-venv python3-pip

Step 2: Create a Project Directory

Navigate to your desired project directory (or create a new one):

mkdir my_project cd my_project

Step 3: Create a Virtual Environment

Create a virtual environment named myenv (you can choose a different name if you prefer):

python3 -m venv myenv

Step 4: Activate the Virtual Environment

Activate the virtual environment with:

source myenv/bin/activate

You should see the environment name in your terminal prompt, indicating that it’s active.

Step 5: Install Packages

Install any packages you need using pip. For example:

pip install requests flask

Step 6: Generate a Requirements File

To create a requirements.txt file that lists all installed packages and their versions, run:

pip freeze > requirements.txt

Step 7: List Installed Packages

To see all installed packages in the virtual environment, use:

pip list

Step 8: Deactivate the Virtual Environment

When you're done working in the virtual environment, deactivate it by running:

deactivate


martes, 17 de septiembre de 2024

list operation

 l1  #list to evaluate

[100, 11, 12, 3, 4, 12, 3, 10, 100, 11, 100, 2, 200]


 // list with  numbers even postion on the list l1

[l1.index(c) for c in [a for a in l1 if  a % 2==0]]

[0, 2, 4, 2, 7, 0, 0, 11, 12]




// list list with the value

[l1[e] for e in [l1.index(c) for c in [a for a in l1 if  a % 2==0]]]


[100, 12, 4, 12, 10, 100, 100, 2, 200]


a dict with index and value

>>> {e:l1[e] for e in [l1.index(c) for c in [a for a in l1 if  a % 2==0]]}
{0: 100, 2: 12, 4: 4, 7: 10, 11: 2, 12: 200}

Or other dict  with  value and index

>>> {l1[e]:l1.index(l1[e]) for e in [l1.index(c) for c in [a for a in l1 if  a % 2==0]]}
{100: 0, 12: 2, 4: 4, 10: 7, 2: 11, 200: 12}

using lambda
>>> test= lambda l1:{e:l1[e] for e in [l1.index(c) for c in [a for a in l1 if  a % 2==0]]}
>>> l2=[100,32,34,13,3]
>>> test(l2)
{0: 100, 1: 32, 2: 34}



>>> test([100,33,53,18,98,67])
{0: 100, 3: 18, 4: 98}
>>> 


domingo, 15 de septiembre de 2024

creating a list if values are or not in ohter list

 


// if   l1  in l2


[a for a in l1 if a in l2]

[11, 90]


//if l1 not in l2


 [a for a in l1 if a not in l2]

[12, 31, 42]


Python docs

 https://docs.python.org/3/


https://docs.python.org/3/library/stdtypes.html#truth-value-testing


https://docs.python.org/3/library/index.html

sábado, 14 de septiembre de 2024

Python main

In Python, `__main__` is a special name used to denote the main entry point of a script. It allows code to determine whether a Python file is being run as the main program or being imported as a module in another script. This distinction is useful for organizing and controlling code execution.


### **Understanding `__main__` in Python**


#### **The `__name__` Variable**


- When a Python file is run directly, its `__name__` attribute is set to `"__main__"`.

- If the file is imported as a module in another script, its `__name__` attribute is set to the name of the file (excluding the `.py` extension).


#### **Common Usage**


The `if __name__ == "__main__":` construct is used to execute code only when the file is run directly, and not when it is imported as a module.


### **Example Usage**


Here’s a simple example demonstrating how to use `__main__`:


```python

# mymodule.py


def greet(name):

    return f"Hello, {name}!"


def main():

    print("This is the main function.")

    name = input("Enter your name: ")

    print(greet(name))


if __name__ == "__main__":

    main()

```


### **Explanation:**


1. **Function Definitions:**

   - `greet(name)`: A function that returns a greeting message.

   - `main()`: A function that contains the main code to be executed.


2. **The `if __name__ == "__main__":` Block:**

   - This block checks if the script is being run directly (`__name__` is `"__main__"`).

   - If true, it calls the `main()` function.


3. **When Imported:**

   - If you import `mymodule` in another script (e.g., `import mymodule`), the `main()` function will not execute automatically.


### **Running the Script**


1. **Direct Execution:**

   - Running the script directly via the command line or an IDE:

     ```bash

     python mymodule.py

     ```

   - This will execute the `main()` function and produce output.


2. **Importing as a Module:**

   - Importing in another Python script:

     ```python

     import mymodule

     

     # This will not execute the main() function automatically

     ```


### **Why Use `__main__`?**


1. **Modular Code:**

   - Allows you to write code that can be both reusable as a module and executable as a standalone script.


2. **Organized Testing:**

   - Helps in writing test code or script-specific code that should not run when the file is imported elsewhere.


3. **Prevent Unwanted Execution:**

   - Prevents the execution of code blocks when the script is used as a module in other scripts, promoting code reuse and modular design.


### **Summary**


- `__main__` is used to define the main entry point of a Python script.

- The `if __name__ == "__main__":` construct helps to differentiate between running a script directly and importing it as a module.

- This practice helps in writing clean, modular, and testable code.

 https://docs.python.org/3/library/__main__.html

jueves, 12 de septiembre de 2024

multi list comprehension

 rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"

num_rainy_months=len([c for c in [float(a) for a in rainfall_mi.split(',') ] if c>3.0])




rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"

num_rainy_months=len([c for c in [float(a) for a in rainfall_mi.split(',') ] if c>3.0])




rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"

num_rainy_months=len([c for c in [float(a) for a in rainfall_mi.split(',') ] if c>3.0])

lunes, 9 de septiembre de 2024

Python Data Structures Cheat Sheet

https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZF9pbnN0cnVjdGlvbnNfdXJsIjoiaHR0cHM6Ly9jZi1jb3Vyc2VzLWRhdGEuczMudXMuY2xvdWQtb2JqZWN0LXN0b3JhZ2UuYXBwZG9tYWluLmNsb3VkL0lCTURldmVsb3BlclNraWxsc05ldHdvcmstUFkwMTAxRU4tU2tpbGxzTmV0d29yay9sYWJzL2hhbmRvdXRzL0NoZWF0X1NoZWV0X1dlZWstMi5tZCIsInRvb2xfdHlwZSI6Imluc3RydWN0aW9uYWwtbGFiIiwiYWRtaW4iOmZhbHNlLCJpYXQiOjE3MTE2Mzg1OTl9.4L5yEN_BpE8odg36ZiLmkRTf3clYER2hiGe0Bu74ljo


https://author-ide.skills.network/render?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZF9pbnN0cnVjdGlvbnNfdXJsIjoiaHR0cHM6Ly9jZi1jb3Vyc2VzLWRhdGEuczMudXMuY2xvdWQtb2JqZWN0LXN0b3JhZ2UuYXBwZG9tYWluLmNsb3VkL0lCTURldmVsb3BlclNraWxsc05ldHdvcmstUFkwMTAxRU4tU2tpbGxzTmV0d29yay9sYWJzL2hhbmRvdXRzL0NoZWF0X1NoZWV0X1dlZWstMl9QYXJ0LTIubWQiLCJ0b29sX3R5cGUiOiJpbnN0cnVjdGlvbmFsLWxhYiIsImFkbWluIjpmYWxzZSwiaWF0IjoxNzExNjM4NTk2fQ.luBpuMXe9qHg0AyG0CeoUDtX9HzrpDcLZMm6gP-p5Dw

viernes, 6 de septiembre de 2024

creating a dict from 2 list

 


users=['ambiorixg12', 'root', 'admin']

sec=[1330, 7813, 1333]






userdb = {a: sec.__getitem__(users.index(a)) for a in users}




Alternative method

userdb = {user: sec[users.index(user)] for user in users}

miércoles, 4 de septiembre de 2024

Getting the key with the max value from a dict

 st

{'Ambiorix': 90, 'Rosa': 78, 'Eddy': 51, 'Maria': 32}




Max values from a  dict


top=[a for a in st.keys() if st[a]==max(st.values())]


['Ambiorix']






[st[a] for a in st.keys()]

[90, 78, 51, 32]








list(st.values())

[90, 78, 51, 32]



list(st.keys())

['Ambiorix', 'Rosa', 'Eddy', 'Maria']

getting to value on a list and its position

 >>> scores =[103, 88, 67, 54, 23, 971, 34, 679]


>>> top=[a for a in scores if a==max(scores)]  ##top socres

[971]

>>> 


>>> pos=[scores.index(a) for a in scores if a==max(scores)]  ##position on the list

[5]