Try Except for one variable in multiple variables

2024/11/17 12:20:31

I am reading every row in a dataframe and assigning its values in each column to the variables The dataframe created using this code

data = [['tom', 10], [, 15], ['juli', 14]]
df = pd.DataFrame(data, columns=['Name', 'Age'])

So after using data.head()

    Name    Age
0   tom     10
1           15
2   juli    14

I want to assign every name to a local variable but what if one name is missing like here how can I do a try and except for this value to assign it automatically 0

try:name1 = ...name2 = ... #What is missingname3 = ...
except:name2 = "Not available"

Remember that if its another name not necessary name 2, what can I do here?

Answer

I'm not sure about the use case, but you could always subclass list so that it works how you're expecting it to. You'd also need to remember to use MyList() as a wrapper around the original list.

class MyCallableList(list):def __call__(self, idx: int, default=0):try:return self[idx]except IndexError:return defaultmy_list = MyCallableList([2, 3, 4, 6, 7])assert my_list(0) == 2
assert my_list(3) == 6
assert my_list(5) == 0
https://en.xdnf.cn/q/120203.html

Related Q&A

Print method invalid syntax reversing a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

How QRegularExpression can be passed to Qt::MatchRegularExpression

I am trying this sample code that I found which is really really good. I am also trying to figure out the same thing to find an item and scroll to it, but this time I wanted to match the the string whi…

Python list rearrange the elements in a required order

My main list has 44 names as elements. I want to rearrange them in a specific order. I am giving here an example. Note that elements in my actual list are some technical names. No way related to what I…

GMB API accounts.locations.reviews.list

Can i get source code to get reviews using gmb python code also heard mybusiness is discontinued. I tried using my business api is discontinued can i get the implementation process to look for extract…

How to get words from an online text archive such as pastebin?

Im trying to get the words(users) from a text file hosted online, such as from www.site.com/mytextfile.txt or pastebin.com/raw/1111111. I will have multiple "users", one in each line. My code…

Python Histogram using matplotlib on top words

I am reading a file and calculating the frequency of the top 100 words. I am able to find that and create the following list:[(test, 510), (Hey, 362), ("please", 753), (take, 446), (herbert, …

how can I add field in serializer?

Below is my serializer.py file: from rest_framework import serializersclass TaskListSerializer(serializers.Serializer):id = serializers.CharField()user_id = serializers.CharField()status = serializers.…

Read and write a variable in module A from module B

Need a solution or workaround to read and write the variable in moduleA from moduleB please. Here is the code:moduleAimport moduleBvariable = 10def changeVariable():global variablevariable = 20def main…

Iterate through a pandas dataframe to get a specific output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 1 year ago.The comm…

Detect if you click inside a box in Python Zelle graphics

I have created a dice betting game. When my code is run, there is a "CLICK TO ROLL" button. Currently, if you click anywhere on the screen, the dice will roll. How can I make it so the progra…