#Python
I have a dataframe: df = pd.DataFrame({'A':[0,0,15,0,0,12,0,0,0,5]})
And I want to replace the 0 value with the nearest non zero value,
For example, the first value is 0, then I find the the nearest non-zero value is 15, so I replace it as 15, then the data becomes:[15,0,15,0,0,12,0,0,0,5],
Then for all the value except first one, I need to find the both side of the nearest non-zero value, and average them. So for the second 0, it would be (15+15)/2; And the third zero would be (15+12)/2
for i in range(len(df['A'])):
if df['A'][i]!=0:#comment these 2 lines if
average is needed for all the elements, and not just the zero
ones
continue
if i==0:#for first element
for r in
df['A']:#iterate through all elements in column A, left to
right
if r!=0:#if a non zero element is found
df['A'][i]=r#assign its value to the first element
break
elif i==len(df['A'])-1:#for last element
for l in
df['A'][-2::-1]:#iterate column A, second last element to the
first
if l!=0:#if a non zero element is found
df['A'][i]=l#assign it to the last element
break
else:#for all the other elements
left=0#variable to hold
1st non zero value on the left side of the current index
right=0#variable to hold
1st non zero value on the right side of the current index
for j in
range(i+1,len(df['A'])):#iterate from index next to current, up to
the end, left to right
if df['A'][j]!=0:#if we find a non zero element
right=df['A'][j]#assign the value to right
break
for k in
range(i-1,-1,-1):#iterate from index one less than current, up to
the first, right to left
if df['A'][k]!=0:#if we find a non zero element
left=df['A'][k]#assign the value to left
break
if left==0:#if left is
0
df['A'][i]=right#assign right to current index
elif right==0:#if right
is zero
df['A'][i]=left#assign left to current index
else:
df['A'][i]=float(left+right)/2#else, assign average
#Python I have a dataframe: df = pd.DataFrame({'A':[0,0,15,0,0,12,0,0,0,5]}) And I want to replace the 0 value...
I have various sums that I've calculated from my dataframe and I want to use matplotlib and make pi chart of percentage of students with a particular nationality. for ex, totalStudents=(df.sum()) studentsSwiss=(df1.sum()) studentsIndian=(df2.sum()) studentsChinese=(df3.sum()) etc. And I want the pi chart to be in percentage. so take in those values convert to percentage of total students of that nationality. I'm using python. additional info: df is a dataframe that has a column that sums total Swiss, Indian, Chinese nationalities and...
Python with Pandas dataframe I have a csv file that contains a large number of columns and rows. I need to write a script that concatenates some elements of the first row with some elements of the 2 row. Something like # if data[1][0] starts with ch then concatenate the element right below it. I have attached a picture of just a sample of my data. The booleans have to stay on there as is. But I must drop the...
(a) Load the data file data/tips.csv into a pandas DataFrame called tips_df using the pandas read_table() function. Check the first five rows. (b) Create a new dataframe called tips by randomly sampling 6 records from the dataframe tips_df. Refer to the sample() function documentation. (c) Add a new column to tips called idx as a list ['one', 'two', 'three', 'four', 'five', 'six'] and then later assign it as the index of tips dataframe. Display the dataframe. (d) Create a new...
// use "for i in range(): //use len. //basic python //discrete mathematics Write a program in Python (in a .py file) that: Makes two lists of 50 numbers each, where the first list is multiples of 5, and the second list is the first 50 squares: 1, 4, 9, 16, 25, 36, ....up to 2500. then I want you to make a third list by multiplying each individual pair in the two lists, so for example: 5 times 1 is...
In the project I am working right now, we have some python and some C# code. At some point, I call from python a subprocess which starts a C# executable. This C# code returns an error code, which has to be understood on the python side and a readable report has to be created. This means, both sides "must speak the same language". So, at the end I have to handle a mapping {error codes: readable explanation} (normally we implement...
I want to write a python function to find the minimum I have an nested list: list = [[0,2,3], [1,6],[4,7]] I want to find out in minimum of each list and compare between each other then return the final minimum result for example: above the nested_list [[0,2,3], [1,6],[4,7,11]], for each set of the list the minimum would be [1,5,3] -> final return would be [1]
I want to replace true with the number of comparisons that were made. For example, if I have a list of [1,4,5,6,7,8] and I ask the program to find 6. is it possible to tell me how many comparisons it took to find 6? PYTHON here is my code: def linear_search(vals,target): for val in vals: if val == target: return True return -1 def binary_search(vals,target): lo = 0 hi = len(vals)-1 mid(lo+hi)//2 while lo <= hi and vals [mid]!=target: if target...
I want this using while loop This using stringin python Use list or some thing in python Using list in python I want answer as soon as posdible E. Last Number time limit per test: 1 second memory limit per test: 256 megabytes input standard input output standard output You are given a sequence of positive integers aj, , 03, ... Print the last element of the sequence. Input The input consists of multiple lines. The i-th line contains a...
Swapping Values in python Summary In this lab, you complete a Python program that swaps values stored in three variables and determines maximum and minimum values. The Python file provided for this lab contains the necessary input and output statements. You want to end up with the smallest value stored in the variable named first and the largest value stored in the variable named third. You need to write the statements that compare the values and swap them if appropriate....
If I have a function in python, say show(B), how would I code it so that if B is a list like [3,3,4,0,0,8,2,0,0,5], it should return the elements in B that are not in range(len(B)). So the length of B in the given example is 10 so the range is from 0 to 9, so we want to return [0,1,2,5,6,9] since these are the values not in B. Thanks