Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python I'm a beginner and I'm learning Python by myself.

CCU0UU

New Coder
I'm a beginner and I'm learning Python by myself.

My current code:

pd.DataFrame(cigarettedata['state'].value_counts())
pd.DataFrame(cigarettedata['price'].value_counts())

Does the price of cigarettes vary by from state to state, what state is best for buying?

How should I write the code for this question? Thank you.
 
If your just learning, I would start with some basic understanding of the language.
  • Learn basic python syntax
  • Learn how to use the print statement
  • Learn about variables
  • Learn about functions
  • Learn about OOP and classes
  • Learn about loops when any why to use
What you provided does not do anything. Your using a pandas dataframe without showing where the data is coming from or how it is to be used.
Just my 2 cents.
 
That's great! Learning a programming language on your own can be a challenging but rewarding experience.
Here is an example of how you could write the code for this question:

Code:
# Create a dataframe with state and price columns
df = pd.DataFrame(cigarettedata[['state', 'price']])

# Group the dataframe by state and find the price statistics
price_stats = df.groupby('state')['price'].agg(['min', 'max', 'mean'])

# Find the state with the lowest average price
best_state = price_stats[price_stats['mean'] == price_stats['mean'].min()].index[0]

# Print the result
print('The best state for buying cigarettes is ' + best_state + '.')

In the meanwhile, you can also view the resources.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom