If condition using Python [closed] - python

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 4 years ago.
Improve this question
Could you please help me generating third column from the table using Python? I tried with numpy.where option, but I am unable to get the desired output.
My table:
I have tried the code
db['Condition'] = numpy.where(db.Value <50, 'Less than 50', db.Value <100, 'Less than 100','more than 100').
Here, db refers to data base name. And the error message I am getting
TypeError: where() takes at most 3 arguments (5 given)

According to numpy.where documentation, it only takes 3 arguments, condition and x (if true),y (if false) array_like. To get your desired output:
db['Condition'] = numpy.where(db['Value'] < 50, 'Less than 50', numpy.where(db['Value']<100, 'Less than 100','More than 100'))

Related

quick sort algorithm by 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 reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried to write some line of code for quick sort algorithm using numpy algorithm.
But It seems not working properly .
Can you help me solve it ?
import numpy as np
def quick_sort_np(narr):
"""A numpy version of quick sort algorithm"""
narr = np.array(narr)
if len(narr)<= 1 :
return narr
p = narr[-1] # I take the last item as pivot by convension
print (f"at this level the pivot is {p}")
lnarr = narr[narr<p]
print (f"----------------------> left arr is {lnarr}")
rnarr = narr[narr>p]
print (f"----------------------> right arr is {rnarr}")
return quick_sort_np(lnarr) + p + quick_sort_np(rnarr)
in case of [1,2,6,5,4,8,7,99,33] as input my code returns nothing and that's the question.
+ acting on np.arrays is element wise addition, not concatenation.

Pandas error: String indices must be integers [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 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()

Custom grouping [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 4 years ago.
Improve this question
I am a Python novice and need help. I tried searching, but couldn't find posts relevant to what I need.
I have a data frame containing a column called diet which contains many similar values like 'Only Vegetarian', 'Mostly Vegetarian', 'Strictly Vegetarian', 'Veggie' etc. How do I combine these values into a single value called say 'Vegetarian'?
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'col1': ['Only Vegetarian', 'Mostly Vegetarian', 'Strictly Vegetarian', 'Veggie','Meat']})
df1['col2'] = np.where(df1.col1.str.contains('Vege'), 'Vegeterian', 'Not Vegeterian')
You can make a dummy variable by encoding your rule in a function and using pd.Series.apply
def check_veg(x):
# The elipse below signifies you providing all the values somehow
if x in ["Veggie", "Mostly Vegetarian", ...]:
return 1
else:
return 0
df["isVeg"] = df["diet"].apply(check_veg)

Pandas Save the results of groupby operation as new column in the original dataframe [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 4 years ago.
Improve this question
I have to set the result of
df.groupby(['région'])['counts'].sum())
as the column c2 of my dataframe.
So I do this:
df['c2'] = pd.to_numeric(df.groupby(['région'])['counts'].sum()).astype(float)
Thus
pd.to_numeric(df.groupby(['région'])['counts'].sum()).astype(float)
has type float, and so df['c2'] should also have type float.
However, when I tried to print the column of my dataframe df['c2'] all values are NaN.
How can I solve this?
EDIT 1:
My code is here
In your code, after this part:
import numpy as np
d_copy = d.copy()
Do this:
d_copy['counts2'] = d_copy.groupby(['region'])['counts'].transform('count')
Results:

subtracting two columns from pandas dataframe and store the result in third column [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 have a DataFrame, df, with 3 columns and I want to perform subtraction as follows:
df['available'] = df['recommended'] - df['manual input']
But I am getting an error stating:
unsupported operand type(s) for -: 'int' and 'str'
I have also tried doing
df['available'] = df['recommended'].sub(df['manual input'])
but it shows the same error.
Also I would like to know that does it returns Series if we try to get particular column from dataframe??
You have to convert values to numeric - e.g. to integers:
df['available'] = df['recommended'] - df['manual input'].astype(int)
Or to floats:
df['available'] = df['recommended'] - df['manual input'].astype(float)
df['available'] = df['recommended'].values - df['manual input'].values

Categories