Pandas function operations [closed]

2024/11/20 16:21:47

Data is from the United States Census Bureau. Counties are political and geographic subdivisions of states in the United States. This dataset contains population data for counties and states in the US from 2010 to 2015.

Which state has the most counties in it? (hint: consider the sumlevel key carefully! You'll need this for future questions too...)

I can not fetch the county name out of the code. Please help

my code:

import pandas as pd
import numpy as np
census_df = pd.read_csv('census.csv')
census_df.head()
def answer_five():return census_df.groupby('STNAME').COUNTY.sum().max()answer_five()
Answer

Here is the answer that worked for me:

def answer_five():return census_df.groupby(["STNAME"],sort=False).sum()["COUNTY"].idxmax()

First part created aggregated df

census_df.groupby(["STNAME"],sort=False).sum()

Second part takes the col you need

["COUNTY"].idxmax()

and returns value corresponding to index with max, check here

https://en.xdnf.cn/q/120761.html

Related Q&A

What is the use of iter in python? [duplicate]

This question already has answers here:Understanding for loops in Python(4 answers)Closed last month.What is the use of using the iter function in python?Instead of doing:for i in range(8):print iI co…

python calculator with two float numbers as parameters [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 7 years ago.Improve…

python - Is it possible to combine 2 functions together to create 1 function? [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 10 years ago.Improv…

How to create a grading system in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python lists comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 1…

Import everyday date for one year

Is there a simple way to import into my Python file a list with all the dates for a given year? What I need is to store everyday that a year has (01.01.2018----31.12.2018) into a list, so I can then i…

Object detection realtime using tensorflow

Im trying to detect objects in realtime using tensorflow. . I ran jupyter notebook in object_detection directory. then I opened the notebook file. It is firing the following errorIm getting the followi…

How do I get Python to read and extract words from a text file? [duplicate]

This question already has answers here:How to copy files(21 answers)Closed 7 years ago.So I need to make a code that opens a txt file, and then takes the content of that file and puts it into another t…

How to draw a checkered flag to the Python screen?

QUESTION: Implement the following pseudocode to draw a checkered flag to the screen.1. Ask the user for the size of the checkered flag (n). 2. Draw an n x n grid to the screen. 3. For i = 0,2,4,...,…

Open txt file in python

i need to open a txt file . In txt file i have Andrei:Popescu:Bucuresti Maria:Popescu:Targu-Mures ....How do I read a text file into three variable and for each line do something ? Sorry for my englis…