Pandas error: String indices must be integers [closed] - python

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 3 years ago.
Improve this question
I am not sure where I went wrong with my below code, where I used two for loops to firstly iterate statename and then iterate each dictionary that contains that specific statename.
I finally resolved this via my second code (the right code on the snip) however would be keen to know why the first didn't work.
The file used is a census file with statename, countyname (a subdivision of the state) and population being the columns.
Couldn't work with the following snip (on the left) where the error is 'string indices must be integers':

As others have already suggested, please read up on providing a Minimal, Reproducible Example. Nevertheless, I can see what went wrong here. When you loop through for d in census_df, this actually loops through the column names for your data frame, i.e. SUMLEV, REGION etc. This is presumably not what you had in mind.
Then your next line if d['STNAME']==c causes an error, as the message says, because string indices must be integers. In this instance you are trying to index a string using another string STNAME.
If you really want that first method to work, try using iterrows:
state_unique=census_df['STNAME'].unique()
list=[]
def answer_five():
for c in state_unique:
count=0
for index, row in census_df.iterrows():
if row['STNAME']==c:
count+=1
list.append(count)
return(max(list))
answer_five()

Don't know why the pic is not coming up...sorry first timer here!
the first code that I tried which I have questions over are: (regarding string indices must be integers):
state_unique=census_df['STNAME'].unique()
list=[]
def answer_five():
for c in state_unique:
count=0
for d in census_df:
if d['STNAME']==c:
count+=1
return list.append(count)
answer_five()
The second code helped resolve my question is:
max_county=[]
state_unique=census_df['STNAME'].unique()
def answer_five():
for c in state_unique:
df1=census_df[census_df['STNAME']==c]
max_county.append(len(df1))
return max(max_county)
answer_five()

Related

array implementation using python from scratch [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 months ago.
Improve this question
My teacher has asked me to implement an array using python without using any inbuilt functions but I am confused and I don't know how to? this is the complete question...
Write a program in Python to implement the “Array” data structure. Perform operations like add or insert, delete or remove and display. Your program should be able to add/insert at any position in an array or remove/delete any element from an array. Take care of extreme conditions such as an empty array or full array and display an appropriate message to the user.
any help would be highly appreciated.
You can store the array in a list L, and write a function for each list operation. For example, to search for an element x in a list L and return the index of the first occurrence of x in L, rather than using the built-in function index, you would implement the linear search algorithm. So, the following code would be incorrect because it uses the built-in function index:
def search(L,x):
return L.index(x)
The following code would be acceptable because you are implementing the linear search algorithm yourself (presumably your teacher wants you to write programs from scratch, which is very good practice):
def search(L,x):
#input: a list L and an element x
#output: the index of first occurrence of x in L, or -1 if x not in L
n = len(L)
for i in range(n):
if L[i] == x:
return i
return -1
L=[3,1,4,2]
print(search(L,7))

Python function that returns independent value based off list input [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.
Improve this question
I have the below JSON response, I want to write a function that:
runs through the response looking for a match of 'risk-level' = [medium OR high]
if match found returns the corresponding alert-id in a list / array format (I think we should use .append here)
if no match is found, exit the program (I'm pretty sure it would "exit()" here)
I have managed to get it to match / find one input and bring back that response, I'm just struggling with feeding it a list with an "OR" logic to bring back an independent result.
[
{'event-num': 5520, 'alert-id': '6e310403-ca53-32ut-aec6-16ffc648f7b7', 'risk-level': 'very-low'},
{'event-num': 5521, 'alert-id': '0a6b15b7-3db3-2x7t-b4ab-b023cfb85eaf', 'risk-level': 'low'},
{'event-num': 5523, 'alert-id': '6e310403-3db3-4b5f-cehd-16ffc648f7b7', 'risk-level': 'medium'},
{'event-num': 5523, 'alert-id': '0a6b15b7-6ty5-4b5f-cehd-b023cfb85eaf', 'risk-level': 'high'}
]
You could use .append() as you mentioned, or, you could do this in a quick list comprehension.
Let's say your list is called events.
risky_events = [event['alert-id']
for event in events
if event['risk-level'] in {'medium','high'}]
The above code simply creates a list of matching risk levels. How would you use the snippet above to implement your exit() requirement? You would need to check if the list created above was empty. Give it a try.
What went wrong in the approach you took? Did you try using .append() yourself? Look up the Python docs section on lists to understand how append works, and give that approach a try.

Save every second line of a file into a list [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 reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to save each second line of a text file into a different element in list. I have studied multiple threads but i need a way so as to use this list and pick a random element from this.
Comprehension as way to solve this:
l = [line for i, line in enumerate(open('list.txt')) if i % 2 == 1 ]
print(l)
Pandas allows you to skip rows according to a function. For example:
import pandas as pd
# read file, excluding even rows
df = pd.read_csv('myfile.csv', skiprows=lambda x: (x+1)%2 == 0)
# convert to list
df_list = df.values.tolist()
This will return a list with elements relating to even lines from the input file.

Getting the following error upon exceuting the below code: Index out of range [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 reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
def evolve():
global pop,fvals
for g in xrange(0,gmax):
for i in xrange(0,NP):
while 1:
r1=random.randint(0,NP-1)
if r1!=i:
break
while 1:
r2=random.randint(0,NP-1)
if r2!=r1 and r2!=i:
break
while 1:
r3=random.randint(0,NP-1)
if r3!=r2 and r3!=r1 and r3!=i:
break
U=[]
V=[]
for j in xrange(0,dim):
U.insert(j,(pop[r3])[j] + F*((pop[r1])[j]-(pop[r2])[j]))
jrand = floor(int(rand1()*dim))
for j in xrange(0,dim):
if rand1()<=cr or j==jrand:
U.insert(j,(pop[r3])[j] + F*((pop[r1])[j]-(pop[r2])[j]))
else:
U.insert(j,(pop[i])[j])
V.insert(i,U)
fvals2.insert(i,fun(U))
x=open("x.out","w")
for i in xrange(NP):
for j in xrange(dim):
print i
print j
x.write(str((V[i])[j]) + '\t')
x.write(str(fvals2[i]))
x.write('\n')
While executing this code block shows an error:
x.write(str((V[i])[j]) + '\t')
IndexError: list index out of range
Here I have taken gmax=5, dim=2 and NP=5.
You're setting V to [] on each of your for n in xrange(0,NP) loops, losing any values it had previously. That means that by the time you're writing the file, V only has one value contained in it (at most), which is why you're getting an IndexError when you try to read xrange(NP) values out of it. You probably want to initialize V outside that loop.
You're also missing some indents on if rand1()<=cr or j==rand, but I'm guessing that happened when putting the code into your post or it probably wouldn't have ran far enough to get the IndexError when writing a file.
That aside, please consider the following:
Please name your variables with actual names - all these single letters and letter-number combinations make the code really hard to read.
You might also consider breaking some of these nested operations up into separate functions, which could also improve readability.
You aren't closing the output file after you're done writing it - you probably want to add an x.close() line to the end or do all the writing within an with open('x.out','w') as x: block. You're also overwriting that file on each of the loops of for g in xrange(0,gmax), so you might want to move it outside that loop as well.

Need to output multiple user-in-putted values from a large CSV to a new CSV [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 8 years ago.
Improve this question
Sorry for the awkward question but I'm having no luck finding a similar example that I can start with before coming here for tweaks/debugging. Is there some specific name for what I'm wanting to do? If I describe what I'm trying to do, can someone tell me what kind of thing of I should be searching for?
I'm trying to write a simple utility in Python that will open a large "database" CSV (3000 rows, each with 30+ columns) and ask the user for some input, either a row number or if they don't know that, a value from column 1. It will then print out all the rows that match it. The user can then enter the row number they desire and repeat until they have all the rows they want, then output to a CSV which will then be converted to JSON format (this part is at least straightforward).
Can someone point me to an example or supply me with the correct terminology for learning this kind of search and output?
The opening of the CSV file and the change from CSV to JSON at the end have examples I can work from.
Thanks!
You can try something a bit like this:
data = [...] # From csv
# Create a dict, with the first column as the key and the row as the value
first_to_row = {cell[0]: cell for cell in data}
result = []
while True:
inp = raw_input(...)
selection = None
# branch based on input
if # done
break
elif # row number
row_number = ...
selection = data[row_number]
else # first column
first_column = ...
selection = first_to_row[first_column]
result.append(selection)
You might want to wrap the data[...] and first_to_row[...] in a try...catch IndexError block.

Categories