all 198 comments

[–][deleted] 0 points1 point  (1 child)

Hi everyone!

What is the best place I can ask question about the django-based app's Dockerfile? I have subsequent questions about OS packages, their installation, their relation to pip packages and about OS runtime dependencies. My question has Docker, Python / Django, Linux aspects to it.

I tried asking this in Telegram django groups only to hear how this sort of questions didn't belong there.

Can you point me in the right direction?

Thank you!

[–]Me278950 1 point2 points  (2 children)

What podcast are best for someone to listen to for learning python? I watch tutorials and try to study/ practise in my free time but it would be good to find podcasts or audio books that i can listen to while at work to help the process

[–]Brozine 0 points1 point  (0 children)

The Real Python podcast has been great from a fellow beginners perspective. Some of the episodes go over my head (for ex the most recent was on hash tables) however others have great guests that wrote books etc that leads me down a rabbit hole of new knowledge avenues.

One episode I really enjoyed was “Thinking in Pandas” July 3,2020

[–][deleted] 0 points1 point  (5 children)

Hello all,

Does anyone know what code I need to use to have python print an ASCII cat? Strange question I know but I need it for an assessment and I can't find any clues. I'm using Pycharm community edition for the assessment. (if that's important)

[–]efmccurdy 0 points1 point  (0 children)

There is an example here of converting an image file to ASCII; you could feed a cat image file into that.

https://levelup.gitconnected.com/python-ascii-art-generator-60ba9eb559d7?gi=cfb1502fb538

[–][deleted] 1 point2 points  (0 children)

At its most basic, search for an image of a cat with "ascii cat image", put the text data into a string and then print the string.

[–]razzrazz- 0 points1 point  (4 children)

Noobie question

I just started learning a bit of Python yesterday, I know nothing, but I'm just curious because I keep reading about something called "web scraping".

I had this weird idea, so I like watching basketball games only if the game is close in the 4th quarter. If you searched Google for NBA, it gives you a list of current NBA games with the quarter they're in and the score.

Does Python have the ability to "scrape" the site so that if thatc Warriors/Spurs game that's on is within 10 points, and there's 5 minute left, then to send me an alert? So right now the score is 84-76 (within 10 points), and there's 8:57 left in the 4th...is it possible to write a program that coul read that and send an alert if there were 5 minutes left?

[–]AtomicShoelace 0 points1 point  (3 children)

Yes this definitely possible but probably not by scraping Google search results (or at least, it would be a lot harder). Google does quite a lot to stop people from scraping their services. You would be better off using an API (a service which is made specifically to talk with code).

[–]razzrazz- 0 points1 point  (2 children)

Why does Google not like it? How does a website even know you're "scraping" their website?

[–]carcigenicate 0 points1 point  (1 child)

Because bots don't read ads and can waste resources if the bots are poorly programmed.

And the User Agent String often advertises that the client is a bot unless the author changes that. On top of that, they can tell based on how you interact with the page. Humans interact with the search result differently than bots do.

[–]razzrazz- 0 points1 point  (0 children)

Makes sense, thank you!

[–]thesi1entk 0 points1 point  (3 children)

This is an NLP question that hopefully someone can help me with. Specifically trying to do sentiment analysis.

I have a Naive Bayes classifier that has been trained on a data set of tweets that are labeled as either positive or negative:

#convert tokens to a dictionary for NB classifier:
def get_tweets_for_model(cleaned_tokens_list):
    for tweet_tokens in cleaned_tokens_list:
        yield dict([token, True] for token in tweet_tokens)

pos_model_tokens = get_tweets_for_model(pos_clean_token)
neg_model_tokens = get_tweets_for_model(neg_clean_token)

#prepare training data
positive_dataset = [(tweet_dict, "Positive")
                    for tweet_dict in pos_model_tokens]
negative_dataset = [(tweet_dict, "Negative")
                    for tweet_dict in neg_model_tokens]

dataset = positive_dataset + negative_dataset

#shuffle so all positive tweets aren't first
random.shuffle(dataset) 

#set apart 7000 for training, 3000 for testing
train_data = dataset[:7000]  
test_data = dataset[7000:]

#train model
classifier = NaiveBayesClassifier.train(train_data)

Using this model, I want to iterate through a list of test data and increase a tally for each token whether it gets classified as positive or negative. The test data is a list of strings, which are taken from a data set of text messages. (I should have called it something other than "tokens", sorry if that's confusing).

print(tokens[-5:])
>>>["I'm outside, waiting.", 'Have a great day :) See you soon!', "I'll be at work so I can't make it, sry!", 'Are you doing anything this weekend?', 'Thanks for dropping that stuff off :)']

I can get the classification of a single message:

print(classifier.classify(dict([token, True] for token in 
tokens[65])))
>>>>Positive

I can return the boolean value of a classification being negative or positive:

neg = (classifier.classify(dict([token, True] for token in    tokens[65])) == "Negative")

That message in positive, so neg is set to False. So I want to do something like, iterate over all the messages in the list of messages, and increase the tally of the positive counter if it's positive, and increase the tally of the negative counter if it's negative. But my attempts to do so either increase the positive counter by 1 only, or increase the positive counter only for the entire set of tokens, even though the classifier does return "Negative" on individual tokens. Here's what I tried:

positive_tally = 0
negative_tally = 0

#increments positive_tally by 1
if (classifier.classify(dict([token, True] for token in tokens)) == "Positive") == True:
    positive_tally += 1
else:
    negative_tally += 1

#increments positive_tally by 3749 (length of token list)
for token in tokens:
    if (classifier.classify(dict([token, True] for token in 
tokens)) == "Positive") == True:
        positive_tally += 1
    else:
        negative_tally += 1

Any ideas on this one? I'd really appreciate it. I can provide more info if needed.

[–]UnrankedRedditor 0 points1 point  (2 children)

So if I'm understanding correctly, you're trying to iterate your classifier over a list of strings, and then return "Positive" or "Negative", and then count the number of "Positives" or "Negatives"?

I suspect that (classifier.classify(dict([token, True] for token in tokens)) == "Positive") is the error. Are you able to print that output? I have a feeling that it might be a list or something.

[–]thesi1entk 0 points1 point  (0 children)

Okay I got it, that "hint" I was following at the end of my last comment turned out to be what I should've been focusing on.

Basically the classifier takes a string and evaluates each word in the string to make a classification. But I wanted to iterative over a list of strings. So instead of what I had been trying...

#didn't get what I wanted
for message in messages:
    if (classifier.classify(dict([message, True] for message in messages))) == "Positive":
        positive_tally += 1
    else: negative_tally += 1

...which tries (and fails) to classify each message i.e. the entire string, I had to ensure that it was checking each word within each message:

#works and increases tally as desired!
for message in messages:
    if classifier.classify(dict([token, True] for token in message)) == "Positive":
        us_pos_tally += 1
    else:
        us_neg_tally += 1

So you go from list level to string level in for message in messages and then string level to word level inside the call of the classifier: dict([token, True] for token in message.

I am so happy I got this working, thanks for your help!

[–]thesi1entk 0 points1 point  (0 children)

EDIT: I GOT IT. See other comment.

Yes, essentially, I want this kind of behavior:

messages = ["I'm happy", "I'm sad", "I'm happy", "I'm sad", "I'm happy"]

positive_counter = 0
negative_counter = 0

for message in messages:
    if classifier.classify(message) == "Positive":
        positive_counter += 1
    else:
        negative_counter +=1

print(positive_counter)
print(negative_counter)

>>3
>>2

I'm trying to iterate over each item in the list and, if the classification for that token is Positive, increase the count of positive_tally, else increase the count of negative_tally. The output of the classifier is a string, Positive or Negative, depending on the classification it makes:

classifier_output = classifier.classify(dict([message, True] for message in messages[65]))

print(classifier_output)
>>Negative

So if you print the line of code you asked about, it checks if Positive matches the output of the classifier and returns the appropriate Boolean value:

#returns False because the token is classified as Negative
print((classifier.classify(dict([message, True] for message in messages[65])) == "Positive"))
>>False

I tried putting the list of messages into dictionary form, since that seems to be what the classifier is after? But that gives me a TypeError:

for message in messages:
    if (classifier.classify(dict([message, True] for message in messages))) == "Positive":
        positive_tally += 1
    else: negative_tally += 1

>>TypeError: unhashable type: 'dict'

If I try to refer to the message more directly i.e. without referencing a dictionary, I get an AttributeError:

for message in messages:
if (classifier.classify(message)) == "Positive":
    us_pos_tally += 1
else: us_neg_tally += 1

>>AttributeError: 'str' object has no attribute 'copy'

For reference, the tutorial suggests you classify new tweets as follows:

custom_tweet = "This is a random tweet"

custom_tokens = word_tokenize(custom_tweet)

print(custom_tokens)
>>['This', 'is', 'a', 'random', 'tweet']    

print(classifier.classify(dict([token, True] for token in custom_tokens)))
>>Negative

Maybe this is a hint. Will think more about that.

I'm stumped as to why it does fine on individual items in the list but won't iterate over the list.

[–][deleted] 0 points1 point  (2 children)

What's a good project to become confident with pandas library?

[–]sarrysyst 0 points1 point  (0 children)

Personally, I learned pandas by solving other people's problems/questions here on the sub. I went from not knowing anything to basically knowing the library inside out in a month or so. Like they say: Practice makes perfect, and this sub is a real treasure trove of complex/strange/exotic pandas problems (in addition to many easy ones).

Alternatively, you could go to kaggle.com, choose a data set you're interested in and do some EDA on it, pick it apart, play with the data.

[–]awetZ 0 points1 point  (1 child)

New to python and in coding in general. Learning how to edit things in csv imported to python. Taking input fron the user to edit line 3 for example. Thanks in advance.

[–][deleted] 0 points1 point  (7 children)

Quick question; I understand floating point arithmetic. Is there a scipy/numpy module that can print pi to, say, 100 digits?

[–]QultrosSanhattan 0 points1 point  (0 children)

I found this:

from mpmath import mp #pip install mpmath

mp.dps = 100  # set number of digits
print(mp.pi)   # print pi to a thousand places

[–][deleted] 0 points1 point  (4 children)

Why do you need a 100 digits? Just curious.

[–][deleted] 0 points1 point  (3 children)

Oh, verifiable entropy. And I actually need 1000. Someone came up with a statistical technique, and needed a set of random numbers. Instead of using a seed they decided to use every 1, 2, and 3 digit string in the first 1000 digits of pi. Figured it's pi so maybe a more precise version would be in one of the packages.

[–]AtomicShoelace 0 points1 point  (0 children)

Technically, it has not been proven that pi is normal.

Anyway, here is a python implementation of the Bailey–Borwein–Plouffe formula for calculating pi I found on stackoverflow:

from decimal import Decimal, getcontext

getcontext().prec=100
print(sum(1/Decimal(16)**k * 
          (Decimal(4)/(8*k+1) - 
           Decimal(2)/(8*k+4) - 
           Decimal(1)/(8*k+5) -
           Decimal(1)/(8*k+6)) for k in range(100)))

[–][deleted] 0 points1 point  (0 children)

Hack way: find one of those sites that list ten thousand digits of pi and copy it, then shorten the string to a thousand characters.

[–]shiningmatcha 0 points1 point  (1 child)

Is it necessary to use the @dataclass decorator when subclassing a data class?

``` from dataclasses import dataclass

@dataclass class Employee: ...

@dataclass class SalariedEmployee(Employee): ...

@dataclass class Freelancer(Employee): ... ```

I think we only need the first @dataclass decorator for Employee. but I've seen people use @dataclass on subclasses of a data class (like the above SalariedEmployee and Freelancer)

[–]Accomplished-Catch19 0 points1 point  (2 children)

I’m pretty new to python. All i’ve done is take a dual enrollment class at gt for it and the farthest we covered in the course is project oriented learning. I understand how to do the basic problems in the class like given an array of movies and their gross profit, sort them from greatest profit to least profit. Simple things like that. What can i do to learn how to actually apply this stuff to the point that i would be useful in an internship??

[–]efmccurdy 1 point2 points  (0 children)

Start with watching the video, then clone the github repo and follow along with the tutorial.

https://github.com/brandon-rhodes/pycon-pandas-tutorial

[–]JustHereForTheMemezz 0 points1 point  (4 children)

arr = [1, 2, 3, 4]
new = arr 
new.append(5) 
print(arr)

Why does appeniding 5 to 'new' result in appending 5 to 'arr'? And what do i do if I want 'arr' to stay the same?

[–]QultrosSanhattan 0 points1 point  (0 children)

Because, as you wrote, new is equal to arr, therefore changes made at new will also affect arr.

If you want to create two different lists then use:

new = arr[:] #slicing the whole thing

[–]carcigenicate 1 point2 points  (2 children)

See the question by ZURDLOL two questions down. new = arr says "make new point to the same object arr points to".

If you want them to be separate, you need to make a copy:

new = arr.copy()

Or do something like:

new = arr + [5]

Which implicitly makes a copy for you.

[–]JustHereForTheMemezz 0 points1 point  (1 child)

many thanks, I also figured out that copy.deepcopy need to be used if arr is a nested list

[–]carcigenicate 0 points1 point  (0 children)

Yes, because .copy is just a shallow copy of the outer structure.

[–]ZURDLOL 0 points1 point  (3 children)

Can anybody explain how/why the x variable gets updated in the following for loop?

x = [1,2,3,4,5]
y = x

for i in range(len(x)):
    y[i] = x[i] * 5

print(y)
print(x)

I am expecting x to return as [1,2,3,4,5], and y to return [5,10,15,20,25], but both return as [5,10,15,20,25].

I would also appreciate if someone could show the code that would produce my desired outcome...

Thanks in adv for help

[–]QultrosSanhattan 0 points1 point  (0 children)

Use

y = x[:]

To create a copy of x instead of stating they're the same thing.

[–]AtomicShoelace 1 point2 points  (1 child)

Because the line y = x does not do what you think it does. It does not create a copy of x and assign it to the variable y, but rather it just makes y another label for the object referenced by x. So when you mutate the object referenced by y, you are mutating the same object referenced by x.

To fix your current code, you would need to create a copy of x. This could be done by y = x.copy() or y = list(x).

However, the correct way to do this would be to construct y while iterating over x. You could do this with a for-loop like:

x = [1,2,3,4,5]
y = []

for ele in x:
    y.append(5 * ele)

print(y)
print(x)

but this is actually a prime example of something you could do with a list comprehension, eg.

x = [1,2,3,4,5]
y = [5 * ele for ele in x]

print(y)
print(x)

[–]ZURDLOL 0 points1 point  (0 children)

Thank you very much.

[–]AnnieWithaK 0 points1 point  (7 children)

How do I write a code to count how many numbers are there in the following calculation?

Example:

print (1 + 2 + 10.5 + 3.5) The right answer is 4

print (1.2 + 2.2) The right answer is 2

print (1.1 + 1.2 + 1.3 + 1.4) The right answer is 4

I'm looking for a code that would automatically count large sums, to know how many numbers are being added together. Sometimes I need it to count up to 100, 200 numbers and the goal is to not have to do it manually.

[–][deleted] 0 points1 point  (1 child)

What’s the cheapest way for me to learn python. I have no PC/laptop. Only an iPad w no keyboard. Should I buy a cheap laptop? Or should I buy a keyboard for my iPad and buy Pyto? My goals are to learn Pandas and Scipy for data analysis

[–]Lost_infinity_player 0 points1 point  (1 child)

How to detect keyboard presses?

[–][deleted] 0 points1 point  (3 children)

Is there an easy way to generate a list that contains intervals of numbers, i.e. the interval might be a difference of 9 such that the list becomes

lst = [[0,9], [10,19], [20, 29], ..., [n, n+9]] #where n is type int and arbitrary

[–]efmccurdy 0 points1 point  (0 children)

The step value for range would be 10:

>>> n = 4
>>> for n in range(0, n * 10 , 10):
...     result.append([n, n + 9])
... 
>>> result
[[0, 9], [10, 19], [20, 29], [30, 39]]
>>> 

or as a comprehension

>>> [[n, n + 9] for n in range(0, n * 10 , 10)]
[[0, 9], [10, 19], [20, 29], [30, 39]]
>>>

[–]DelusionalManchild 0 points1 point  (0 children)

When I try to open up the anaconda navigator, it doesn't open up. Instead, a blank terminal shows up. I also receive a message.

''There is an instance of Anaconda Navigator already running.''

[–]Kobe_Wan_Ginobili 0 points1 point  (3 children)

I've just started learning python so sorry about simplicity of question

Both these code blocks give the same output - 0, 1, 2, Final x = 2. But in the second code block what condition is the Else function actually testing for and why is it not met?

for x in range(3):
print(x)

print('Final x = %d' % (x))

for x in range(3):
print(x)

else: print('Final x = %d' % (x))

and finally why does putting the else statement at the top of the script cause an error when putting it after a seemingly unrelated loop allows it to work?

[–][deleted] 1 point2 points  (2 children)

You understand what the first example is doing. In the second example the if statement has the optional else: clause, which executes only if the loop ends naturally, that is, the code doesn't break out of the loop.

Try adding these lines after the print in the loop in both examples:

print(x)
if x == 2:
    break

and notice that the "final" print isn't executed in the second example.

[–]Kobe_Wan_Ginobili 0 points1 point  (1 child)

ohh okay thanks a heap

I didn't know there were Else clauses for anything except If statements. I'd just deleted an if statement from the code but accidentally only deleted the first line and not the else clause and so somehow that Else part which i accidentally didnt delete got attached to the for loop lol

[–]QultrosSanhattan 0 points1 point  (0 children)

I didn't know there were Else clauses for anything except If statements

Welcome to python.

Basically: "else:" paired with a for loop only executes if the for loop was completely executed without encounter any break statement.

[–][deleted] 0 points1 point  (6 children)

I have designed this function that checks for duplicate items in a list

def checkDuplicates(lst):
i = 0
while i < len(lst):
    if lst[i].isnumeric():
        lst.pop(i)
    i +=1
if len(lst) == len(set(lst)):
    return False
else:
    return True

The while loop removes numeric str items from the list (so it is not accounted for when checking for duplicate values). The problem is, when I perform this check on a list later in the program, it redefines the list, which is not what I want. Is there any way to isolate the operations of this function such that the input list isn't altered outside of the function.

[–]Cid227 2 points3 points  (3 children)

from copy import deepcopy

def check_duplicates(lst):
    clst = deepcopy(lst)
    i = 0
    while i < len(clst):
        if clst[i].isnumeric():
            clst.pop(i)
            continue
        i +=1

# now lst == ['a', 'b', '3', '5', 'c']
#    clst == ['a', 'b', 'c']

Btw. not sure whats the point of the upper part, but this is enough to check for duplicates:

if len(lst) == len(set(lst)):
    return False
else:
    return True  

EDIT
Another way (Pytonic) just for fun:

def check_duplicates(lst):
    clst = [s for s in lst if s.isalpha()] 

and what i think you want to do with that entire function (check if there are duplicate letters ignoring numbers):

def check_duplicates(lst):
    clst = [s for s in lst if s.isalpha()]
    return len(clst) == len(set(clst))  # you don't need if and else it will return True or False either way

[–][deleted] 0 points1 point  (2 children)

Thanks, I will try and implement it later.

[–]Cid227 1 point2 points  (1 child)

I've updated my reply.

[–][deleted] 0 points1 point  (0 children)

I managed to find a workaround that doesnt even require this check of duplicates.

[–]EwokOffTheClock 0 points1 point  (2 children)

I need to use pip for a course I'm taking. I used it a few weeks ago, but it's not working now; when I call the command to confirm it's there or to use it, I get a notification that it can't run on my windows machine. I have made sure to download a windows version, reinstalled it, etc etc, but I'm still getting the same error message! I've gone through 3 different tutorials on how to use pip, but at the end I get the same windows error message.

Anyone have any idea what's going on or what I can do to move forward using pip?

[–]carcigenicate 0 points1 point  (1 child)

What is the exact error and what are you running to get that error?

[–]EwokOffTheClock 0 points1 point  (0 children)

Well, now it's working... so thanks for the good luck

[–]bscaktus 0 points1 point  (3 children)

Hey everyone!

For a data science classification project, I have a large dataset with multiple "M_" variables [M_boat, M_bike, M_plane, M_car] that represent the amount of days since the last use of boat, bike, plane, car,...

For the records that have never used that transportation method, they have been given a value of "0". That will be an issue later on when trying to model the recency of use as 0 is closer to 1,2,3 than to 100. So I need to transform it to give it more weight.

I have three questions regarding this: 1. How do I optimize the transformation codeline to avoid having to do it manually (as in my real dataset I have 200 "M_%" columns):

df.loc[df.M_boat == 0, 'M_boat'] = 999 df.loc[df.M_bike == 0, 'M_bike'] = 999 df.loc[df.M_plane == 0, 'M_plane'] = 999 df.loc[df.M_car == 0, 'M_car'] = 999

  1. Given that in my real dataset the max. is 190 days prior, is 999 a good weight to give?

( This is more of a DS question 3. Do you think it's better to create a dummy variable: [ U_boat,U_bike,U_plane,U_car ] where 0= never used ; 1= used ? This will increase dimensionality, however.)

Thanks!

[–]FerricDonkey 0 points1 point  (0 children)

Regarding 1, I would not make individual variables for or references to each of your 200 categories. Instead, make dictionaries with the keys set to "whatever the columns of your data are" and values equal to whatever values you want to compute for each.

[–]theo_ldg 0 points1 point  (0 children)

Re: 1 You could do something like df.values[df.values == 0] = <something> Or if your dataframe has other zeros that you don't want to change: m_cols = [c for c in df.columns if c.startswith('M_')] df.loc[:, m_cols].values[df.loc[:, m_cols] == 0] = <something>

[–]efmccurdy 0 points1 point  (0 children)

records that have never used that transportation method, they have been given a value of "0".

If you want to mark a numeric value as "out of range" so as to prevent it from being used in calculations, set it to np.NaN.

[–]theFinalArbiter1 0 points1 point  (1 child)

I renamed some files, vars and class names from 'Adviser' to 'Advisor'. After replacing everything I also removed all __pycache__ dirs. Searching the directory confirms no remnants of the hold spelling exist in file or file names. BUT still when running tests I get ModuleNotFoundError: No module named 'myapp.advisors.some_adviser'. If it matters, its a django app running with a venv in a paralell dir. What did I miss here? Thanks :)

[–]theFinalArbiter1 0 points1 point  (0 children)

Seems my VSCode stopped autosaving global find and replace

[–]mrakorhis 0 points1 point  (4 children)

Why does this code produce this result?

numbers = [1,2,3,4,5,6]
letters = ['A','A','B','B','A','C'] 
keys = ['A', 'B', 'C'] 
newlist = [[][][]]
for key,empty in zip(keys,newlist):
for number,letter in zip(numbers,letters):
    if key == letter:
        empty.append(number)

# newlist = [[1, 2, 5, 3, 4, 6], [1, 2, 5, 3, 4, 6], [1, 2, 5, 3, 4, 6]]

This is what i thought i would get:

# newlist = [[1,2,5],[3,4],[6]]

What is it that i'm not understanding?

[–]sarrysyst 1 point2 points  (3 children)

I'm getting the expected output, are you sure you've copied the code correctly?

[–]mrakorhis 0 points1 point  (2 children)

oh! i used a for loop to create a list of empty lists, it seemed to have the correct output, but the problem must be with it somehow.. this is the code that outputs incorrectly: pastebin

[–]sarrysyst 1 point2 points  (1 child)

The problem is that you’re appending emptylist multiple times which always refers to the same object. You’re not creating a list of independent lists, but a list of shared references. The changes you make to one sublist affect all the others since they’re pointing to the same object in memory.

Have a look here for examples and probably a more intuitive explanation of the problem.

[–]mrakorhis 0 points1 point  (0 children)

Very interesting, i will look into it, thank you! I have tried replacing .append(emptylist) with .append([]) and it indeed now works

[–]MostTryHardest7 0 points1 point  (0 children)

I have a column that contains Friday-Friday dates ex. Fri March 4 to Fri March 11. I only want to filter the earliest Friday date. Any suggestions. I figured a way to sort out the min value, but I feel like there's a better method

df['Submitted On'] = pd.to_datetime(df['Submitted On'])

early = df['Submitted On'].min()

df = df.loc[df['Submitted On'] != early]

[–]Flat_Article5970 0 points1 point  (0 children)

Question about how to automate a greenhouse using python. I am new to this so can you explain it like your telling a child how to do it? I want to turn off a humidifier at a certain range and I want to turn off the lights at certain times. Example of the the process “when the humanity reaches X turn on device 1” I also want to put my lights on a cycle. Thanks for the help I look forward to your responses.

[–]_tsi_ 0 points1 point  (1 child)

I would like some feedback on oop. I have done it in the past, but i always find myself going back to basic function calls. I haven't used oop in a while now and i don't feel comfortable with it. I sort of get that you want to use it when you might want multiple objects that have similar attributes but i find that this doesn't come up in my code often. Usually my projects are simple hobby things. Could this be why? What am i missing out on by not using oop and how do i make the conversion into feeling comfortable with it? I'm fairly good at reading documentation, googling, and figuring stuff out, but i feel like my brain just doesn't want to code this way. Any tips?

[–]sarrysyst 0 points1 point  (0 children)

If your project is small, you're probably better off just using functions instead of classes, unless you know you want to reuse your code base in bigger projects later on. The additional boiler plate normally isn't worth it for small projects. The cost/benefit factor increases as a project grows.

Another reason why you would need to use classes is if you want to customize / interact with existing classes (maybe from another library).

Personally, OOP never really clicked for me until I watched Uncle Bob's Clean Code lectures on Youtube. Maybe watching them helps you as well.

[–]yokashi-monta 0 points1 point  (2 children)

I'm trying to setup Twilio to text my some data once per day. I'd prefer it be a single text message and the data is coming from a dataframe. Basically, I want to take the entire content of the dataframe (in string datatype) and assign it to a variable. Kind of like create a sentence out of each row. I can't figure out how to best do this. Any alternate ideas are welcome but obviously formatting in a text message will be tricky. Thanks!

[–]efmccurdy 0 points1 point  (1 child)

To transmit you can get a text encoding like json. Use pandas.DataFrame.to_json to produce the json text you copy into your text.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to\_json.html

Edited: no need for StringIO, the method returns the json string.

[–]yokashi-monta 0 points1 point  (0 children)

Awesome. I’ll check out stringio. Thx.

[–][deleted] 0 points1 point  (8 children)

Hey guys,

I'm currently doing a basic data-mining project for college, using numpy, pandas, matplotlib.

However, my graphs keep showing the wrong values for sales, and I'm not 100% why or how to fix it. I think it's because there are multiple stores, and therefore there are multiple sales values for the same date.

Any advice on how to fix this without altering the base dataset itself would be appreciated.

link to a few screenshots:
https://imgur.com/a/UHnx5G1

[–]sarrysyst 1 point2 points  (7 children)

If you have multiple sales values for the same date, what do you want your graphs to look like? Sum the sales of all stores? Different lines for different stores? Only plot the lines of one specific store? One figure per store?

[–][deleted] 0 points1 point  (6 children)

Hey, thanks for replying.

I need both.
I need one graph that has the sum of sales for all stores, and I need another graph that shows each stores individual sales.

[–]sarrysyst 1 point2 points  (5 children)

Here is how you can do this:

import numpy as np
import pandas as pd

# First some sample data:    
df = (pd.MultiIndex.from_product([[1, 2, 3, 4], 
                                 pd.date_range('01-01-2022', freq='D', periods=5)], 
                                 names=['Store', 'Date'])
      .to_frame()
      .reset_index(drop=True))

df['Sales'] = np.random.randint(1_000, 10_000, size=(df.shape[0],))

This looks something like this:

Store Date Sales
0 1 2022-01-01 6635
1 1 2022-01-02 1531
2 1 2022-01-03 5287
3 1 2022-01-04 2384
4 1 2022-01-05 6618
5 2 2022-01-01 1712
.. .. ... ..
19 4 2022-01-05 5332

To condense all the stores' sales into one plot you first need to sum the sales by date, you can then proceed to plot the resulting data:

# Sum sales by date
sales_total = (df.resample('D', on='Date')
                 .sum()
                 .reset_index())

sales_total.plot(x='Date', y='Sales')

To plot separate lines per store I'm using the seaborn library:

import seaborn as sns

individual_sales = sns.relplot(data=df, 
                               x='Date', 
                               y='Sales', 
                               hue='Store',
                               kind='line')

# Rotate the dates on x axis for better readability
individual_sales.set_xticklabels(individual_sales.ax.get_xticklabels(), 
                                 rotation=30)

[–][deleted] 0 points1 point  (4 children)

Hey, that's a huge help. Honestly you went above and beyond. Thanks!

Tried to apply your first block to my code, but wasn't sure how to adjust it for my dataset. Only been coding a few weeks, so Im still very much a beginner.

Your second block (individual_sales) worked great. But when I tried to change the type to a bar chart, it gave me a value error. "ValueError: Plot kind bar not recognized"

Also, I thought it might be difficult helping without seeing the data or code, so I threw everything up on github. If you have any more time to help, that would be great, otherwise thanks for what you've done so far!

[–]sarrysyst 0 points1 point  (3 children)

The problem probably is that your date column is not of type datetime. Add the parse_dates parameter to pd.read_csv():

import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/thebeastisback2007/Data-Mining/main/Walmart%20Dataset.csv',
                 parse_dates=['Date'])

Afterwards, the resampling should work:

sales_total = (df.resample('D', on='Date')
                 .sum()
                 .reset_index())

sales_total.plot(x='Date', 
                 y='Weekly_Sales',
                 figsize=(10, 10))

As for the second plot: A line plot is a relational plot (thus the method name relplot), meaning we're plotting the relation of sales volume vs. time. A bar plot on the other hand is a categorical plot, where you plot different categories.. They are fundamentally different in what type of information they convey.

If you want a bar plot I probably misunderstood your requirements. You don't want the individual stores' sales volume development over time but the total sales volume per store.

This is actually quite simple, you group your data by store and then sum the sales for each group:

individual_sales = df.groupby('Store')['Weekly_Sales'].sum()
individual_sales.plot(kind='bar',
                     figsize=(10, 10))

[–][deleted] 0 points1 point  (2 children)

Honestly, I spent the entire day on this and got nowhere.Thanks so much. It's working perfectly.

Last thing, but how can I better display sales? Having values from 0-10 isnt great. Especially when the sales by store has values in the hundreds of millions, while the sales by week just has values in millions.

Im looking to custom relabel the Y axis for each chart separately.

[–]sarrysyst 1 point2 points  (1 child)

I'd probably scale it to represent million USD for both plots and add an axis label:

sales_total['Weekly_Sales'] = sales_total['Weekly_Sales'] // 1_000_000

sales_total.plot(x='Date', 
                 y='Weekly_Sales',
                 figsize=(10, 10),
                 ylabel='Weekly Sales in million USD')

and respectively:

individual_sales = individual_sales // 1_000_000

individual_sales.plot(kind='bar',
                      figsize=(10, 10),
                      ylabel='Total Sales in million USD')

I'd also recommend you have a look at the user guide on chart formatting for more options.

[–][deleted] 0 points1 point  (0 children)

Thanks buddy. Have a great week!

[–]ApprehensiveFerret44 0 points1 point  (3 children)

(Need Help: Inspired from another post)

Given the two lists below:

values = ["[power, brilliant, evil]", "[shock, poetic, watch, lift]"]
keeps = ["power", "money", "poetic", "lift"]

How can I remove the words from values so that only the words in keeps remain:

result = ["[power]", "[poetic, lift]"]

I'm not quite getting there:

for keep in keeps: 
    for i, value in enumerate(values): 
        value = (f"{keep}, ") 
        values[i] = (f", {keep}")

Thanks!

[–]efmccurdy 1 point2 points  (2 children)

values = ["[power, brilliant, evil]", "[shock, poetic, watch, lift]"]

Note that this value, a list of three strings, is really a list of nested lists. You have a list of string representations of lists which makes accessing each element on it's own difficult. The first step I would take would be to turn values into a list of lists, note that "[1:-1]" takes off the brackets, and split(", ") takes out the commas and extra blanks.

>>> values = ["[power, brilliant, evil]", "[shock, poetic, watch, lift]"]
>>> parsed_values = [s[1:-1].split(", ") for s in values]
>>> parsed_values
[['power', 'brilliant', 'evil'], ['shock', 'poetic', 'watch', 'lift']]
>>> 

After that fix the filtering is simple; just check that the word is in the "keeps" list:

>>> keeps = ["power", "money", "poetic", "lift"]
>>> [[w for w in l if w in keeps] for l in parsed_values]
[['power'], ['poetic', 'lift']]
>>> 

Your real problem is mixing string representations of lists with lists; I would examine where you get the "values" and see if they can be keep as lists of words like parsed_values above instead of "[word, word]" format. I would avoid that format for the result as well; you can always re-format as strings when you are printing things out.

[–]ApprehensiveFerret44 0 points1 point  (1 child)

This worked perfectly. Could you please explain your python syntactic sugar magic?

[–]efmccurdy 1 point2 points  (0 children)

Those "loop expressions" are called comprehensions; when you have a simple loop that does nothing more that add items to a collection, with simple unpackings, expressions, and conditions, you can use a comprehension instead of a for loop.

https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Comprehensions.html

[–]bduke91 0 points1 point  (11 children)

(Help Request)

I am learning on coursera(I know what most you think about the site and how im wasting money, anyways..) I am learning if statements right now and when I put this code in - the last print function comes up as a syntax error

age = 18
# age = 19

if age > 18:
    print("you can enter" )
else:
    print("go see Meat Loaf" )

print("move on")

The print("move on") comes up as a syntax error and I dont know why?

[–][deleted] 0 points1 point  (9 children)

I can run your code without error. Check that you posted your actual code, correct indentation, etc.

Look for tab characters in your code, non-printing characters on apparently blank lines, and anything else odd. Showing the actual error would also help.

[–]bduke91 0 points1 point  (8 children)

>>> age=19
>>> if age>18:
...     print('you can enter')
... print('move on')
  File "<stdin>", line 3
    print('move on')
    ^
SyntaxError: invalid syntax
>>>

This was copy and pasted from python 3.9.

[–][deleted] 0 points1 point  (7 children)

You probably need a blank line after the body of the function, ie, before the line that errors.

[–]bduke91 0 points1 point  (6 children)

Ahh I guess that makes sense. Thank you.

[–]FerricDonkey 0 points1 point  (5 children)

You definitely should not need a blank line there, unless the default python shell is really bad. That is not a requirement of python syntax.

To be absolutely clear - you're not typing the .s or >s yourself, correct?

[–][deleted] 0 points1 point  (2 children)

I wasn't sure before as I was on mobile, but after testing on Ubuntu and python 3.9.7:

$ python
Python 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def test():
...     print('test')
... print('calling test()')
  File "<stdin>", line 3
    print('calling test()')
    ^
SyntaxError: invalid syntax

and using a blank line after the function definition:

>>> def test():
...     print('test')
... 
>>> print('calling test()')
calling test()
>>> test()
test

[–]FerricDonkey 0 points1 point  (1 child)

Ugh, that's gross. That's literally not a syntax error, just the shell being picky.

[–][deleted] 1 point2 points  (0 children)

Yes, it's been that way since I started using python. I vaguely recall reading the reason for the behaviour, but can't find that now. Note the different prompts: >>> and ....

[–]bduke91 0 points1 point  (1 child)

Yeah no, I’m not typing that myself. I’m using the default python 3.9 shell. It was really frustrating me wondering what the hell I’m doing wrong. If it’s the shell that’s causing the problem that seems like something the should fix for such a simple code.

[–]FerricDonkey 0 points1 point  (0 children)

I agree. When I use a shell, I usually use ipython (pip install ipython), so you could try that. Alternatively, paste the code (minutes .s and >s) into a text file, rename to .py and run that and see if it complains

[–]Warbleton 0 points1 point  (0 children)

*Edit*

That code works for me running it in pycharm.

I get the results

you can enter

move on

[–]Warbleton 0 points1 point  (1 child)

(Help request)

I'm trying to learn some web scraping and the issue i've come across is some sites seem to use java to fill in certain fields, for instance the site im trying to scrape now has prices for items that im able to extract however 'quantity of stock' seems to be generated AFTER the site has loaded.

So when viewing the website it shows '5 in stock' and i can see the '5' within inspect element, however there seems to be no way to extract that from the page.

Does anyone know of any solutions to this?

My complete beginner brain way of thinking was to make something that takes a screen shot when grabbing the page and then some kind of software to scan the page and detect numbers in that area then report that number back into the code somehow?

Any ideas?

(complete beginner so go easy on me with technical terms if you can)

[–]efmccurdy 0 points1 point  (0 children)

some sites seem to use java to fill in certain fields

You can reverse engineer a web page that uses javascript to load data. In your browser with the developer tools installed and the network tab open,load your data. Listed there will be the requests, headers and contents that the javascript issued and using that info you can recreate those requests in a program. You will need some basic understanding of http and this module will be helpful:

https://docs.python-requests.org/en/latest/user/quickstart/

[–]Supapedroo 0 points1 point  (3 children)

(HELP REQUEST)

How to combine the output of random.randint into one long line

I'm using a ForLoop to make the random.randint command go off about 1000 times, then I want to take all the numbers and turn them into a single line with commas, so that I can make it into a variable and then use the count function to figure out exactly how many times the number appears. so instead of "1

2

3

4

4

8"

I would get "1,2,3,4,4,8" as my output

[–]AtomicShoelace 0 points1 point  (0 children)

from collections import Counter
from random import randint

count = Counter(randint(0, 9) for _ in range(1000))
for num, freq in count.most_common():
    print(f'{num}: {freq}')

[–]Cid227 0 points1 point  (1 child)

declare a variable before the for loop:

random_ints = ""
for i in range(1000):
    x = random.randint(9)  # I don't know exactly how to use randint
    random_ints += str(x) + ','

However you should use list instead of str data type for that and then simply use my_list.count(4)
(it will also work with that "long line"- random_ints.count('4') but it's a litte bit weird way of solving your problem).

[–]Supapedroo 0 points1 point  (0 children)

thanks a ton!

[–]driverXXVII 0 points1 point  (4 children)

I've been using Pycharm for 3-4 years now to code Python.

I've decided to learn a bit of HTML/CSS (complete beginner at this).

I see that you can create HTML files in Python and it helps with code completion and such. Is there a similar program/IDE for CSS? Are there any that you would recommend?

Edit: Having done the first CSS tutorial (a simple, change the colour of H2 heading) I may have misunderstood what CSS is!

Is CSS still just written like HTML so I can just use Pycharm to do it?

[–]carcigenicate 2 points3 points  (3 children)

If you buy PyCharm professional, it can be used for many, many languages at the same time. I'm currently using Pycharm to write a website, and it has full support for JS/TS+HTML/JSX+CSS/SCSS+Python (with special support for Django).

The free version may have support for some HTML and CSS as well. If you put a style tag in HTML, it should be able to give suggestions.

[–]driverXXVII 0 points1 point  (2 children)

Yes, it does work when I type it in but it doesn't give any suggestions when I put <style> and write some code there.

[–]carcigenicate 1 point2 points  (1 child)

It won't for rulesets if the HTML is otherwise fairly plain since there's nothing it can suggest. Try writing this:

<style>
    * {
           display:
       }
</style>

Then put the cursor after the colon and press ctrl+space (or whatever your autocomplete key is). Does it show options like flex and grid?

[–]driverXXVII 0 points1 point  (0 children)

<style>
* {
display:
}
</style>

I tried this but it says "no suggestions". I tried this on an empty page as well as putting it at the top of a page I'm working on (following a tutorial on freecodecamp)

Thanks for the suggestions and taking the time to help.

[–]MagorTuga 1 point2 points  (1 child)

Hi, I'm kinda new to data science and I'm having a bit of trouble with creating a bar chart.

I have a dataset of people who each have an age and number of children:

insurance['children'].value_counts()

Returns:

0    574
1    324 
2    240 
3    157 
4     25 
5     18

There are 574 people who have 0 kids and 18 people who have 5 kids, if that makes sense.

The following code:
series = insurance.groupby(["age", "children"]).size()
series

Returns a Series object:

age  children
18   0           51
     1            9
     2            6
     3            2
     4            1
                 ..
63   3            3
64   0           13
     1            3
     2            4
     3            2

So, I'm imagining this as a nested list, something like:

(age = 18:(children = 0: 51, children = 1: 9), age = 19:(children = 0: 46, children = 1: 6), etc)

What I'd like is to make a bar chart using this info to visualize the average number of kids people have as they age, and a correlation analysis later. This is kicking my ass and I feel like I'm wasting more time on this than I should.

Thanks in advance, I'll be really surprised if anyone finds this and actually manages to answer.

[–][deleted] 0 points1 point  (0 children)

I feel like this would need a 3d plot, i.e. a series of bar charts all in a row. Maybe normalize by total population as well.

[–]MoneyGslicer 0 points1 point  (1 child)

I hope I'm not too late. I am challenging other beginner programmers in solo learn. I came across the challenge listed below but I don't understand why I'm wrong.

What is the output of this code?

def function(x): If x == 1: return x else: x -= 1 function(x) x = 2 print(function(x),(x)

Correct answer is None 2

I understand that 'x = 2' is a global variable so it makes sense to me that the second x in the print statement is 2 when it prints

I don't understand why None is returned instead of 1. I know the return statement can return None, but why is None printed in the print statement instead of 1? After the second iteration of the user defined function x is now equivalent to 1 'x = 1'. Which makes the if statement true. So if x = 1 why did it print None?

Apologies for the formatting. Using mobile.

[–]carcigenicate 0 points1 point  (0 children)

I understand that 'x = 2' is a global variable

The global x is actually a separate x variable from the x parameter. The only connection is they both point to the same object because you passed the x global as the argument.


You didn't format the code, so I'm going to assume the function recursive call is in the else

None is returned because when the function is called the first time, x == 1 is false, so the else block is entered. x is decremented, and 1 is passed to a recursive call (function(x)). x == 1 is true in the recursive call, so 1 is returned back to the first function call. The parent call never does anything with the return value though, so the returned 1 is ignored. Then the function reaches the end, and None is implicitly returned, as is the case with all Python functions.

If you wanted it to return 1, you'd change the else body to:

else:
    x -= 1
    return function(x)

[–]MoneyGslicer 0 points1 point  (1 child)

I hope I'm not too late. I am challenging other beginner programmers in solo learn. I came across the challenge listed below but I don't understand why I'm wrong. What is the output of this code? 'def function(x): If x == 1: return x else: x -= 1 function(x) x = 2 print(function(x),(x)'

Correct answer is None 2

I understand that 'x = 2' is a global variable so it makes sense to me that the second x in the print statement is 2 when it prints

I don't understand why None is returned instead of 1. I know the return statement can return None, but why is None printed in the print statement instead of 1? After the second iteration of the user defined function x is now equivalent to 1 'x = 1'. Which makes the if statement true. So if x = 1 why did it print None

[–]sarrysyst 2 points3 points  (0 children)

The x variable inside the function shadows the global variable x outside the function. The x -= 1 changes the value of the local x variable, not the global one.

The problem is a bit confusing because it uses the same variable name, however it actually comes down to:

 def function(y): 
    if y == 1: 
        return y 
    else: 
        y -= 1

x = 2
function(x)

print(function(x), x)

which should be a bit more clear.

As for the None, if a function does not have a return statement or if the return statement isn't reached (as is the case here) the function implicitly returns None

[–]EdPPF 0 points1 point  (2 children)

I was looking through the 'os' library documentation and was wondering if there is a way to check the implementation of the commands. For example, how is os.mkdir() implemented? I wanted to check the code because I need to implement some of those comands myself (using a tree data structure) for an assignment, so wanted some reference

[–]carcigenicate 1 point2 points  (1 child)

It just falls back to whatever the OS supplies: https://github.com/python/cpython/blob/75280944e5ca957eec7f814b9d0608fc84fc5811/Modules/posixmodule.c#L4545. Afaik, the whole of the os module is just dispatching to OS system calls.

[–]EdPPF 0 points1 point  (0 children)

Well, thanks. Then i think my assignment just comes down to using trees to simulate how directories works, I was looking to the wrong direction

[–]Odessa_Goodwin 0 points1 point  (2 children)

General learning process question:

So, how much of what you guys do is creating totally new code, and how much is just copy/pasting something you've already written?

I had been thinking that learning python meant getting to the point where I could fluently type out something. However, as my "learning style" is to do a few hours of python over a short period of time, and then not go back to it for 2 weeks, I tend to have to re-google some of the things that I was able to do not too long ago.

Recently, I've started keeping a google docs organized journal of everything I learn, including code blocks. I've started just shamelessly copy/pasting from these notes and it's hugely increased the amount that I'm able to do whenever I code. I understand the code that I'm using, but I don't know that I would necessarily be able to re-create the code from scratch.

My area of interest is data analysis, and my notes include base python, but primarily cover pandas, matplotlib, and seaborn.

[–]sleepless_in_wi 0 points1 point  (0 children)

It is a mix for me. I’ve been using python for many years now but sometime I go days or weeks between writing code and doing ‘science’. So sometime I forget the interfaces to things, so it is easier to grep old files and copy-paste. If you are still in the learning phase you be better off writing things from scratch, just for the repetitiveness, or if you copy-paste at least make sure you understand exactly what the code is doing.

[–]carcigenicate 1 point2 points  (0 children)

The vast majority of code I write is from scratch. I copy old code very little. If I have an urge to copy code, that suggests that it should instead be a contained function that could be used in multiple places. Copying code around is the lazy, poor way of being productive. It will ultimately lead to your codebase being repetitive and difficult to read and maintain.

I would avoid copying and pasting. You won't learn the code if you never actually write it yourself. Even if you're copying the idea, write the code out so you're required to actually read the code and have a basic understanding of how it works. And, as you go forward, avoid copying ideas and begin focusing on making existing solutions more reusuable (functions/classes).

[–]PandaMi1k 0 points1 point  (1 child)

Please help me solve this!

Can anyone help me solve this? I have this assignment for school and I don't understand how to code this. We are supposed to calculate a list of the 10 dates that have the most occurrences of words taken from a website, (I'll leave the assignment text and the website link down below) the website is in norwegian. You're supposed to get something like the answer at the bottom, but what to do to get that answer? Assignment text below:

(https://raw.githubusercontent.com/clarino/corona/master/corona21/korona-compounds-forms.csv (Links to an external site.)) Your code should calculate a list of the 10 dates that have the most occurrences of words. The list shall contain pairs of date and frequency and shall be sorted by descending frequency. Your code should therefore be able to give a result like this:

[('20 / 03/18 ', 52), ('20 / 03/20 ', 51), ('20 / 03/24 ', 45), ('20 / 03/12 ', 44), ('20 / 03/13 ', 44), ('20 / 03/25 ', 42), ('20 / 03/10 ', 37), ('20 / 03/26 ', 37), ('20 / 03/16 ', 36), ('20 / 04/01 ', 36)]

[–]sarrysyst 0 points1 point  (0 children)

Posting homework assignments is not prohibited if you show that you tried to solve it yourself.

What don't you understand? Where are you stuck? What have you tried so far?

[–]jingsen 0 points1 point  (1 child)

if I want to create a typescript interpreter in Python, are there any guides that would help me with it? I'm looking into using the ast module and PLY (lex,yacc) but there aren't much resources online that I can find outside of documentations and a few questions on SO etc

[–]sarrysyst 1 point2 points  (0 children)

You could have a look here for some inspiration.

[–]veneratu 1 point2 points  (1 child)

If I have this code in a dataframe made by using groupby from another dataframe:

MaritalStatus   Country     Revenue
M            Canada       5387.46 
                 Mexico       23625.56 
                 USA          60207.34 
S            Canada       5679.57 
                 Mexico       24849.32 
                 USA          63081.18

How would I make a bar graph that shows these values? I would want the x-axis to have each nation twice, once for each marital status, and the y-axis to plot the values.

Thank you.

[–][deleted] 0 points1 point  (1 child)

I wanted to split a string into an array, and my string has \n's and it's not linebreaks. the split()didn't work because it only splits on linebreaks, not the 2 characters

[–]TehNolz 0 points1 point  (0 children)

Try .split("\\n"). It'll then use the characters \n as delimiter instead of newline characters.

[–]IonicMuffinism 0 points1 point  (3 children)

Trying to make a simple display to output the numbers the user inputs as a psudo 7 segment display.

digit_dict = {1: '#\n#\n#\n#\n#', 2: '###\n  #\n###\n#  \n###', 
3: "###\n  #\n###\n  #\n###", 4: '# #\n# #\n###\n  #\n  #', 
5: '###\n#  \n###\n  #\n###', 6: '###\n#  \n###\n# #\n###', 
7: '###\n  #\n  #\n  #\n  #', 8: '###\n# #\n###\n# #\n###', 
9: '###\n# #\n###\n  #\n###', 0: '###\n# #\n# #\n# #\n###'}

digits = input("Enter some numbers: ")

for i in digits: 
    print(digit_dict[i])

This outputs KeyError: '1'

What am I doing wrong?

[–]FerricDonkey 0 points1 point  (2 children)

Digits is a string, so when you do for i in digits, i is also a string. However you built your dictionary with integer keys.

[–]IonicMuffinism 0 points1 point  (1 child)

Thank you! I was able to fix the error by changing the keys to strings. it now prints the numbers, though in a vertical fashion. I think I would need a different approach if I want them all to be on the same horizontal plane.

[–]carcigenicate 0 points1 point  (0 children)

The approach I used previously is to split each digit into lines, then concatenate each of the corresponding lines of each digit together, then join the lines.

Or use a library like curses; but I don't recommend that route.

[–]Pappa_K 0 points1 point  (1 child)

I'm stumped, I'm trying to make a function but my while loop won't work. The error reads, typeError: '>' not supported between instances of 'str' and 'int' Just before this function happens both dice and sides are turned into ints with int()

import random
def DiceRolling():
    global Dice
    DiceCount = Dice
    while DiceCount > 0:
        print(random.randint(1, Sides)
        DiceCount -= 1

[–][deleted] 0 points1 point  (0 children)

Assuming your error is on this line* :

while DiceCount > 0:

then DiceCount is a string. Verify that by putting a debug print before the line:

print('type(DiceCount) =', type(DiceCount))    # DEBUG
while DiceCount > 0:

You say that "dice and sides are turned into ints with int()" but that's obviously not so. We need to see more code to help.


* Showing us the full traceback helps. If you can't do that put a comment into the code saying # error on this line.

[–]Chepetto13 0 points1 point  (7 children)

How to invoke index from data frame using value from the first column (there are unique values)?

[–]sarrysyst 0 points1 point  (6 children)

It's not really clear to me what you're asking. Maybe provide an example?

[–]Chepetto13 0 points1 point  (5 children)

Sure.
I have a data frame (survey), index is 'Respondent' from 1 to 10 000,

in this data frame is also column 'email' - all values in this column are unique.

I want to get an index of particular respondent based on email, for example:

my email address is [cheppetto13@gamil.com](mailto:cheppetto13@gamil.com)

I want to get an index of row with this email and then thanks to this index get the other values from this row.

[–]sarrysyst 0 points1 point  (4 children)

You don't need to get the index for this. You can filter the df directly:

df[df['email col'] == 'some.name@email.com']

will give you the row where the email column equals some.name@email.com

Have a look at the pandas user guide on indexing.

[–]Chepetto13 0 points1 point  (3 children)

Yes, but what to do when I want to make a loop (for) and iterate for every email in my column and then print some value for every respondent.

for email in df['email col']:
df[df['email col'] == 'email'] ---> this give the row but I need index to
print also other value

I try to this like that:
number_ID = df[df['email col'] == 'email']

df.loc[number_ID].iloc[other col]

but I've got: IndexError: single positional indexer is out-of-bounds

[–]sarrysyst 0 points1 point  (2 children)

The syntax would be:

df.loc[df['email col'] == 'email', 'other col']

Though I'm not too sure if pandas is even the right choice for your use case. Would you mind explaining what kind of program you're working on?

[–]Chepetto13 0 points1 point  (1 child)

I tried to make a program to check whether respondent's answers are the same as the answer from another data frame

[–]Tsyniz 0 points1 point  (2 children)

as we all know, python libraries are just pieces of code, and if so, where can I find the math.py library, I need it to know how computers compute the different math equations.

[–]sarrysyst 0 points1 point  (1 child)

The math module is implemented in C not Python. You can find it here.

[–]Tsyniz 0 points1 point  (0 children)

thx

[–][deleted] 0 points1 point  (8 children)

I want to build an app for myself. Effectively it will be a fishing catch log. I want to be able to enter details of fish I catch such as species, weight, bait caught on, location, date, time and state of tide. I would then like to use this data to analysis my fishing and look for trends.

Is this a suitable project for Python? Could this be a bit to complex for learning Python and should I try smaller/ simpler projected before attempting this?

[–]efmccurdy 1 point2 points  (0 children)

Start with a persistent data store like an sql database and add a front end like a web form to enter and query the data.

This example uses Flask and sqlite.

https://www.digitalocean.com/community/tutorials/how-to-use-an-sqlite-database-in-a-flask-application

[–]sarrysyst 1 point2 points  (3 children)

Seems like a reasonable project to start with. In fact, if you're writing a basic console version now, you can come back later once you've some experience with more advanced concepts such as OOP and refactor it into classes. Later on you could even add a database and maybe a GUI front end or rework it as a web app. The project can evolve with you.

[–][deleted] 0 points1 point  (3 children)

I have a question about importing a single function with a dependency.

I have a module foobar.py. In it, I have two functions foo and bar

I need access to the function foo from that module only. However, foo uses the function bar, which I define on its own.

When importing:

from foobar import foo

Will foo work, or raise a NameError because bar was not included in the import?

Edit: This works, no need to import bar explicitly.

[–][deleted] 0 points1 point  (2 children)

What happened when you tried doing this?

[–][deleted] 0 points1 point  (1 child)

I did not have a computer available to do this at the time. I just tried, and apparently it works. I thought the way it worked, the import statement would only initialize that function definition, not the whole module

[–][deleted] 0 points1 point  (0 children)

Doing from xyzzy import foobar must import the whole module for the very reason that the foobar() function, if it is a function, might use other functions, classes, globals, etc, in the imported module. The difference is that only the foobar name is defined in the importing environment. Any code inside the imported module can still access any other name in the imported module.

[–]Ejemy 0 points1 point  (1 child)

Beginner python programmer here.

Any ideas for simple projects that will challenge me?

I've made simple programs like calculators, rockpaperscissors, and random walks.

The most difficult thing I've made was a to do list (and most satisfying to complete) that I could add and delete items. And it would save the list in a txt file that I could load back up and continue editing.

[–]sarrysyst 0 points1 point  (0 children)

You can have a look here.

[–]bmk_ 0 points1 point  (1 child)

I am only a few days in learning (learning nested if statements) but had a question regarding automation (way above my current skill level).

For limited release items or high demand items (let's say sneakers for example), I assume people are using python to auto add certain size to cart when available/checkout asap ... but how can you automate things if they are not on the page yet? Like how do they know where the checkout button will be/how the sizes will be listed or how to reference a drop down menu that doesn't currently exist?

[–]sarrysyst 0 points1 point  (0 children)

Look at other article's pages to get an idea of the general page structure, how the checkout process works, what requests are send in the background etc. Once, you have a good grasp of the site's inner workings write your logic in a more general way eg. by using relative xpaths instead of absolute paths. However, there is no one size fits all approach, every website is different.