Python - how to reassign the cells' values in a DataFrame given a list ? - finding a fast way to achive it in a big data table - python-3.x

I have a big table with the size of 5,905,635*30 (see figure 1), and a list with the size 5,905,635 of rows (see figure 2). I want to reassign the cells' values in the table, given the elements of the list (see figure 3).
figure 1
figure 2
figure 3
For example, like the codes below, I want to get df2 given df1 and list1; an easy way is to loop the elements of list1, the first element is 'B', so assign the first row of column B to 1 in df1, and the second element is 'C', then assign the second row of column C to 1, and etc. The final result should be df2. The problem with this solution is too slow if I have a big size of table. I wonder if there is a fast way to achieve this goal.
df1 = pd.DataFrame({'A': ['0', '0','0', '0', '0', '0', '0'],
'B': ['0', '0','0', '0', '0', '0', '0'],
'C': ['0', '0','0', '0', '0', '0', '0'],
'D': ['0', '0','0', '0', '0', '0', '0'],
'E': ['0', '0','0', '0', '0', '0', '0']})
list1 = ['B','C','A','E','D','A','D']
df2 = pd.DataFrame({'A': ['0', '0','1', '0', '0', '1', '0'],
'B': ['1', '0','0', '0', '0', '0', '0'],
'C': ['0', '1','0', '0', '0', '0', '0'],
'D': ['0', '0','0', '0', '1', '0', '1'],
'E': ['0', '0','0', '1', '0', '0', '0']})
The problem of this solusion is too slow if I have a big size of the table. I wonder if there is a fast way to achieve this goal.

Related

While swapping in python using rindex for this specific case why swapping is not happening?

While swapping in python using rindex for this specific case why swapping is not happening? Case-1 is giving correct ans, but not case-2.
Case-1:
S=['1', '1', '1', '2', '1', '1']
S[S.index('1')], S[''.join(S).rindex('2')] = S[''.join(S).rindex('2')], S[S.index('1')]
print(S)
Output: ['2', '1', '1', '1', '1', '1']
Case-2:
S=['2', '1', '1', '1', '1', '1']
S[S.index('1')], S[''.join(S).rindex('2')] = S[''.join(S).rindex('2')], S[S.index('1')]
print(S)
Output: ['2', '1', '1', '1', '1', '1']
But Expected Output: ['1', '2', '1', '1', '1', '1']
Calculate your indices before doing the swap, not in the middle of it:
index1, index2 = S.index('1'), ''.join(S).rindex('2')
S[index1], S[index2] = S[index2], S[index1]

Trying to separate items in list by character but output returns multiple times

I am trying to separate items in a list by character, and this is done but whenever i run the code it separates the items but shows them separated multiple times. How can I fix this?
I've already tried using range in a for function, but that hasn't worked. The only thing that gives an output is using
for character in x
My code:
def rle():
askq = int(input("How many lines of RLE compressed data do you want to enter?"))
if askq < 2:
print("You must enter at least 2 lines of RLE compressed data.")
rle()
print("Please enter your RLE compressed data one line at a time")
lines = []
for i in range (0, askq):
i = input("Which lines would you like to convert?")
lines.append(i)
num=0
lines_input = [1,num]
lines2 = []
x = []
for i in range(0,askq):
num+=1
if num in lines_input:
x.append(lines[i])
for x in lines:
for character in x:
lines2.append(character)
print(lines2)
rle()
I expect the output of
lines2
to be
["0","1","d","6","1"," ","0","1","b"]
but instead i get
['0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b']
['0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b']
['0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b', '0', '1', 'd', '6', '1', ' ', '0', '1', 'b']
Try this update
def rle():
askq = int(input("How many lines of RLE compressed data do you want to enter?"))
if askq < 2:
print("You must enter at least 2 lines of RLE compressed data.")
rle()
print("Please enter your RLE compressed data one line at a time")
lines = []
for i in range (0, askq):
i = input("Which lines would you like to convert?")
lines.append(i)
new_list = []
for i in lines:
new_list.extend(list(i))
print(new_list)
rle()

How can a return statement of a Python function keep strings in the same line like print(argument, end='')?

I am fresh off the coding boat and I am attempting to have the return values of a python function all print on one line. I know to use print(argument, en= ''), although I cannot include a print function in the return of a defined function.
Ultimately I am trying to get each first item of a list in a list, then the second and so on to print the design turned 90 degrees. I am first getting each list in a for loop then getting each index from there and printing in a second for loop.
With the code the way I have it now, I get the result I need, but there is "None" at the end of each line because I have a print function in a function instead of a "return" so I can have each index value printed on one line per list. I am curious about how to have the values displayed on one line without using the end argument of a print function in a function return. And I would always love to see a better/faster way to achieve this result.
..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....
Thank you!
grid = [['.', '.', '.', '.', '.', '.',],
['.', '0', '0', '.', '.', '.',],
['0', '0', '0', '0', '.', '.',],
['0', '0', '0', '0', '0', '.',],
['.', '0', '0', '0', '0', '0',],
['0', '0', '0', '0', '0', '.',],
['0', '0', '0', '0', '.', '.',],
['.', '0', '0', '.', '.', '.',],
['.', '.', '.', '.', '.', '.',]]
xLength = len(grid[0])-1
yLength = len(grid)-1
listX = list(range(0, xLength))
listY = list(range(0, yLength))
def listGrid(y = 0):
for x in grid:
print(x[y], end = '')
for num in listX:
print(listGrid(num))
You need to return a value from the function - if a function does not return anythin, it returns None implicitly. Your code prints the "data" inside the function and you print the return of the function by
print(listGrid(num)) # prints None
Print the results of your function outside, using end="" is an option:
grid = [['.', '.', '.', '.', '.', '.',],
['.', '0', '0', '.', '.', '.',],
['0', '0', '0', '0', '.', '.',],
['0', '0', '0', '0', '0', '.',],
['.', '0', '0', '0', '0', '0',],
['0', '0', '0', '0', '0', '.',],
['0', '0', '0', '0', '.', '.',],
['.', '0', '0', '.', '.', '.',],
['.', '.', '.', '.', '.', '.',]]
xLength = len(grid[0]) # fix, do not subtract 1 - range is upper border exclusive
yLength = len(grid)-1
listX = list(range(0, xLength))
listY = list(range(0, yLength))
def listGrid(y = 0):
return [x[y] for x in grid] # return a list
for num in listX:
for r in listGrid(num):
print(r, end="")
print()
Or you leverate zip() to make columns from your rows and print them:
# directly operates on your data - you do not need anything of your code beside
# the grid definition
for c in zip(*grid):
print(''.join(c))
Output:
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....

Appending a sublist to another list in python

with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
nofinding=[]
rawrow=[]
for row in datarows:
if row[1]=='No Finding' and row[2]=='1':
rawrow = list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]))
nofinding.append(rawrow)
print(nofinding[:2])
I am reading datarows from a csv file and want to create a customized nested list based on certain columns. I want that
list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]))
shall return a list like
['00030805_000.png,No Finding,1,34777,69373']
which is stored in rawrow and then append to a bigger list i.e. nofinding but i am getting output as
[['0', '0', '0', '3', '0', '8', '0', '5', '', '0', '0', '0', '.',
'p', 'n', 'g', ',', 'N', 'o', ' ', 'F', 'i', 'n', 'd', 'i', 'n', 'g',
',', '1', ',', '3', '4', '7', '7', '7', ',', '6', '9', '3', '7', '3'],
['0', '0', '0', '3', '0', '8', '0', '4', '', '0', '0', '0', '.', 'p',
'n', 'g', ',', 'N', 'o', ' ', 'F', 'i', 'n', 'd', 'i', 'n', 'g', ',',
'1', ',', '3', '5', '4', '0', '5', ',', '6', '3', '0', '8', '8']]
Desired output
[ ['00030805_000.png,No Finding,1,34777,69373'], ['00030804_000.png,No
Finding,1,35405,63088'] ]
Thank you
Your issue is that rawrow = list((row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18])) is turning the string in to a list of characters
if you want to leave this as a comma delimited string replace that line with the following:
rawrow = row[0]+","+row[1]+","+row[2]+","+row[17]+","+row[18]
or more cleanly:
rawrow = ",".join([row[row_index] for row_index in [0, 1, 2, 17, 18]])
I am curious though why you want:
[ ['00030805_000.png,No Finding,1,34777,69373'], ['00030804_000.png,No Finding,1,35405,63088'] ]
Instead of this:
[ ['00030805_000.png','No Finding',1,34777,69373], ['00030804_000.png','No Finding',1,35405,63088] ]
which you could achieve with the following:
rawrow = []
for row_index in [0, 1, 2, 17, 18]:
rawrow.append(row[row_index].split(","))
or in one line:
rawrow = [row[row_index].split(",") for row_index in [0, 1, 2, 17, 18]]
Furthermore, your whole code could be consolidated as follows:
with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
nofinding = [",".join([row[row_index] for row_index in [0, 1, 2, 17, 18]]) for row in datarows if row[1]=='No Finding' and row[2]=='1']
print(nofinding[:2])
with open('LBP_for_paper.csv','r') as csvDataFile:
datarows = csv.reader(csvDataFile, delimiter=',', quotechar='|')
rawrow = []
nofindings=[]
for row in datarows:
if row[1]=='No Finding' and row[2]=='1':
rawrow = [''.join(row[row_index]) for row_index in [0, 1, 2, 17, 18] ]
nofindings.append(rawrow)
print(nofindings[:3])
Solved my issues.

List to string to lists of letter

I am trying to convert a list of strings to a list of letters/numbers but keeping the length of list the same. Here is my list look like,
a = ["0587828028", "2967480535"]
My code to convert the above list to split the string and save in a new list.
new_a = []
for i in range(len(a)):
new_a += a[i]
And the output is on list,
['0', '5', '8', '7', '8', '2', '8', '0', '2', '8', '2', '9', '6', '7', '4', '8', '0', '5', '3', '5']
Desired output should be 2 list:
['0', '5', '8', '7', '8', '2', '8', '0', '2', '8'] ['2', '9', '6', '7', '4', '8', '0', '5', '3', '5']
Any suggestion is much appreciated, I am very new in python.
Just use the built-in list() iterable expansion:
a = ["0587828028", "2967480535"]
new_a = [list(x) for x in a]
# [['0', '5', '8', '7', '8', '2', '8', '0', '2', '8'],
# ['2', '9', '6', '7', '4', '8', '0', '5', '3', '5']]

Resources