How to compare the first column to the second column - python

I'm having a problem on my schoolwork. My goal is I want to compare the first column, first row and second column, second row.
I'm new to pandas so I have tried browsing answers on the internet but sadly there isn't. I have think of a way which is I have a converted pandas into a array and store the first column to the first array and second column to the second array.
first_array = [4,10,17,24,82,93,35,40,49,71,78]
last_array = [9,16,23,29,89,97,39,48,57,77,85]
and after that i've popped the first number of first_array which is 4 then insert a 0 into the last which looks likes this
first_array = [10,17,24,82,93,35,40,49,71,78, 0]
last_array = [ 9,16,23,29,89,97,39,48,57,77,85]
and a little bit of for loop which is
if first_array < last_array:
print ("===")
but it seems that my logic is correct but there's a slight wrong with the output i'm expecting
['10', '17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['9', '16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
===
['17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
['24', '82', '93', '35', '40', '49', '71', '78', '0']
['23', '29', '89', '97', '39', '48', '57', '77', '85']
['82', '93', '35', '40', '49', '71', '78', '0']
['29', '89', '97', '39', '48', '57', '77', '85']
['93', '35', '40', '49', '71', '78', '0']
['89', '97', '39', '48', '57', '77', '85']
['35', '40', '49', '71', '78', '0']
['97', '39', '48', '57', '77', '85']
===
['40', '49', '71', '78', '0']
['39', '48', '57', '77', '85']
['49', '71', '78', '0']
['48', '57', '77', '85']
['71', '78', '0']
['57', '77', '85']
['78', '0']
['77', '85']
['0']
['85']
===
At the first "===" 10 isn't < 9 which makes me think. Did i miss something? Thank you in advance.
This is a snippet of my code. The print(join_tag) is the output i needed for this.
normalized_text = []
first_array = [10,17,24,82,93,35,40,49,71,78, 0]
last_array = [ 9,16,23,29,89,97,39,48,57,77,85]
for word in normalized_text:
join_tag = ' '.join(word)
print (join_tag)
if first_array < last_array:
print ('===')
first_array.pop(0)
last_array.pop(0)
Expected output would be
['10', '17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['9', '16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
['17', '24', '82', '93', '35', '40', '49', '71', '78', '0']
['16', '23', '29', '89', '97', '39', '48', '57', '77', '85']
['24', '82', '93', '35', '40', '49', '71', '78', '0']
['23', '29', '89', '97', '39', '48', '57', '77', '85']
['82', '93', '35', '40', '49', '71', '78', '0']
['29', '89', '97', '39', '48', '57', '77', '85']
['93', '35', '40', '49', '71', '78', '0']
['89', '97', '39', '48', '57', '77', '85']
['35', '40', '49', '71', '78', '0']
['97', '39', '48', '57', '77', '85']
===
['40', '49', '71', '78', '0']
['39', '48', '57', '77', '85']
['49', '71', '78', '0']
['48', '57', '77', '85']
['71', '78', '0']
['57', '77', '85']
['78', '0']
['77', '85']
['0']
['85']
===

if I understood your question correctly, you want to compare the 4 with the 16, right?
If so, putting the data back in a dataframe:
import numpy as np
import pandas as pd
first_array = np.array([4,10,17,24,82,93,35,40,49,71,78])
last_array = np.array([9,16,23,29,89,97,39,48,57,77,85])
df = pd.DataFrame(np.vstack((first_array, last_array)))
Then:
df.iloc[0, 0] < df.iloc[1, 1]
Will compare 4 < 16
To compare them all:
for i in range(len(df.columns)-1):
print(df.iloc[0, i] < df.iloc[1, i+1])

Your arrays contain strings. Convert them to numbers.
In the first case '10' < '9' is true because the string '10' is alphabetically less than the string '9'

Related

Is there a way to ordinate numbers in a list?

I'm working on a table game called 'tombola' (I don't know the name in English).
The code gives me a list of numbers:
['22', '25', '75', '52', '70', '14', '5', '60', '81', '83', '72', '2', '36', '78', '10', '65', '43', '74', '51', '9', '29', '49', '24', '76', '23', '67', '35', '8', '85', '59', '18', '66', '38', '27', '19', '57', '77', '42', '84', '11', '46', '13', '89', '62', '7', '39', '32', '50', '86', '44', '64', '79', '54', '12', '68', '34', '15', '69', '71', '45', '20', '41', '82', '16', '1', '48', '37', '58', '61', '56', '53', '40', '80', '31', '87', '73', '90', '3', '88', '55', '30', '21', '4', '63', '26', '28', '33', '6', '17']
I need to ordinate these numbers in a crescent way and have an output that looks something like this:
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90']
it seems like you have a list of strings and you want to sort them numerically. to do this you need to tell sort that it needs to sort each element as if it were an int.
my_nums = ['22', '25', '75', '52', '70', '14', '5', '60', '81', '83', '72', '2', '36', '78', '10', '65', '43', '74', '51', '9', '29', '49', '24', '76', '23', '67', '35', '8', '85', '59', '18', '66', '38', '27', '19', '57', '77', '42', '84', '11', '46', '13', '89', '62', '7', '39', '32', '50', '86', '44', '64', '79', '54', '12', '68', '34', '15', '69', '71', '45', '20', '41', '82', '16', '1', '48', '37', '58', '61', '56', '53', '40', '80', '31', '87', '73', '90', '3', '88', '55', '30', '21', '4', '63', '26', '28', '33', '6', '17']
my_nums.sort(key=int)
print(my_nums)
OUTPUT
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90']

How do I avoid this error for this piece of code-TypeError: 'NoneType' object is not subscriptable

def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
print(open_csv()[1])
This is the output for open_csv():
['Name', 'Gender', 'Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5', 'Test 6']
['Aisha', 'F', '0', '33', '67', '27', '12', '14']
['Alex', 'M', '12', '90', '34', '56', '93', '39']
['Bala', 'M', '13', '25', '58', '17', '49', '29']
['Denise', 'F', '13', '93', '84', '53', '65', '62']
['Farhan', 'M', '15', '5', '10', '62', '34', '11']
['Gopi', 'M', '21', '61', '39', '32', '91', '32']
['Irfan', 'M', '26', '36', '3', '95', '36', '39']
['Jun Ming', 'M', '29', '86', '77', '6', '91', '61']
['Lily', 'F', '30', '34', '46', '96', '100', '44']
['Mei Ling', 'F', '39', '58', '9', '61', '32', '46']
['Muthu', 'M', '39', '60', '13', '69', '55', '100']
['Nurul', 'F', '50', '35', '4', '27', '11', '97']
['Priya', 'F', '50', '25', '47', '15', '35', '86']
['Siti', 'F', '58', '71', '13', '19', '58', '30']
['Elisa', 'F', '59', '22', '73', '52', '77', '49']
['Dennis', 'M', '65', '94', '83', '67', '37', '22']
['Harry', 'M', '74', '75', '76', '82', '57', '1']
['Gary', 'M', '90', '12', '70', '86', '50', '59']
['Terry', 'M', '93', '84', '26', '99', '90', '72']
['Corinne', 'F', '100', '17', '88', '14', '33', '9']
When I run this code, I get TypeError:"NoneType" object is not subscriptable. Why is this so and how do I fix it?
If you want to print the second row in your CSV file, you could modify your function as follows:
def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r', newline='') as f:
return list(csv.reader(f))
print(open_csv()[1])
This will return all the data as a list of rows, by adding [1] you will be displaying the second row (with [0] being the first row). The reason you were getting the error is that your code did not have a return statement in the function. The default is for Python to return None. So in effect your code was doing:
print(None[0])
Don't forget to add newline='' when using it for a csv.reader()
Turn this function into a generator.
def open_csv():
...
for row in reader:
print(row) // change this to yield(row)
list(open_csv())[0]

How do I do something to each element in a list?

I am currently making a bingo game that calls a number when enter is pressed. It does this until the player's bingo card is empty.
I can not get it so that it calls the first number in the list of numbers and goes on to the second then the third and so on without making a script for each of the 99 numbers.
What I want to do is get the program to find the first element of nums and then get the program to print the string and if the string is in the player_1 list then remove the string from player_1 and keep on doing the process until either the program has gone through all the strings or the player_1 list has no strings left in the list.
Here is my code:
import random
def player_1_card():
print(player_1[1:4])
print(player_1[5:8])
print(player_1[9:12])
print(player_1[13:16])
return
nums = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']
random.shuffle(nums)
player_1 = random.sample((nums), 16)
while len(player_1) != 0:
input("Press <enter> to call a number")
for i in nums:
print("Your current bingo card:")
player_1_card()
print("Number called is", # )
input("Press <enter> to call a number")
if nums(#) in player_1:
nums.remove(#)
I have placed hashes where I need some help with the code.
I am using Python 3.5.2.

TypeError: unsupported operand type(s) for /: 'float' and 'str' - Python/Matplotlib [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the two following lists:
list1=['21', '28', '28', '18', '17', '17', '18', '16', '20', '21', '22', '22', '20', '17', '23', '21', '20', '21', '21', '22', '22', '22', '21', '22', '21', '20', '21', '20', '22', '21', '24', '24', '23', '23', '23', '22', '22', '22', '23', '24', '22', '23', '24', '20', '22', '23', '24', '23', '24', '22', '25', '26', '22', '21', '21', '21', '21', '20', '21', '23', '23', '23', '24', '21', '26', '26', '27', '26', '26', '29', '27', '26', '25', '27', '27', '26', '26', '24', '24', '23', '26', '24', '26', '29', '29', '28', '27', '27', '26', '26', '26', '28', '25', '26', '26', '24', '25', '25', '26', '25', '25', '25', '26', '31', '25', '24', '24', '24', '24', '24', '25', '24', '22', '26', '27', '26', '27', '28', '25', '28', '27', '28', '27', '29', '28', '28', '29', '28', '28', '25', '27', '27', '27', '27', '27', '29', '31', '30', '28', '28', '27', '28', '28', '27', '26', '28', '27', '25', '27', '25', '27', '26', '26', '27', '28', '32', '28', '26', '27', '26', '25', '25', '26', '25', '26', '25', '27', '26', '26', '25', '28', '28', '29', '29', '31', '34', '33', '31', '32', '32', '30', '32', '32', '31', '32', '33', '35', '36', '37', '35', '35', '34', '33', '32', '32', '34', '32', '32', '32', '31', '32', '31', '33', '31', '33', '33', '34', '37', '32', '32', '32', '32', '32', '30', '34', '35', '33', '35', '34', '32', '36', '38', '38', '38', '37', '37', '38', '39', '37', '38', '38', '39', '39', '39', '38', '39', '38', '41', '39', '43', '44', '44', '45', '44', '42', '42', '44', '43', '41', '43', '43', '43', '40', '41', '39', '41', '39', '40', '40', '45', '42', '41', '40', '40', '41', '40', '42', '42', '38', '42', '44', '45', '45', '45', '44', '46', '48', '47', '50', '51', '50', '51', '49', '50', '48', '49', '50', '51', '50', '52', '53', '56', '57', '55', '54', '53', '55', '53', '54', '51', '53', '52', '51', '49', '48', '48', '49', '50', '47', '48', '51', '53', '53', '50', '47', '47', '48', '47', '48', '47', '49', '48', '52', '60', '58', '55', '100', '61', '55', '55', '54', '54', '54', '55', '55', '54', '53', '54', '53', '55', '56', '54', '55', '56', '58', '56', '56', '56', '55', '54', '55', '56', '56', '56', '56', '53', '54', '54', '54', '54', '52', '52', '54', '57', '58', '55', '54', '52', '52', '52', '54', '55', '54', '53', '53', '56', '57', '56', '56', '58', '55', '61', '59', '60', '61', '60', '64', '61', '62', '61', '60', '62', '63', '63', '65', '67', '69', '71', '68', '69', '66', '68', '70', '69', '69', '70', '69', '67', '68', '66', '65', '66', '63', '63', '64', '66', '72', '68', '65', '62', '64', '61', '63', '60', '59', '62', '63', '64', '65', '63', '61', '64', '63', '61', '61', '63', '64', '64', '63', '62', '65', '64', '64', '64', '64', '68', '66', '68', '69', '72', '70', '68', '66', '68', '65', '71', '69', '62', '63', '64', '61', '63', '64', '64', '64', '62', '60', '62', '70', '66', '65', '62', '62', '62', '62', '60', '64', '64', '64', '66', '66', '63', '69', '68', '66', '67', '69', '68', '67', '67', '68', '66', '68', '67', '68', '70', '71', '69', '70', '70', '75', '75', '73', '69', '71', '69', '69', '69', '73', '71', '71', '69', '67', '67', '65', '68', '69', '64', '66', '65', '70', '66', '66', '64', '63', '65', '64', '65', '66', '65', '65', '66', '68', '65', '65', '69', '66', '68', '65', '70', '69', '71', '69', '71', '72', '69', '68', '67', '68', '71', '72', '74', '76', '72', '71', '72', '70', '69', '67', '70', '70', '66', '66', '68', '67', '66', '67', '65', '74', '68', '67', '69', '69', '68', '69', '67', '67', '66', '64', '63', '66', '66', '66', '66', '66', '64', '62', '66', '66', '65', '66', '68', '70', '69', '68', '67', '67', '67', '64', '67', '66', '68', '66', '69', '68', '69', '69', '65', '66', '69', '68', '69', '66', '65', '65', '66', '66', '65', '66']
list2=['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', '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', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3', '4', '3', '3', '2', '2', '3', '2', '2', '3', '3', '2', '2', '2', '2', '2', '2', '3', '2', '2', '2', '3', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '2', '2', '2', '2', '2', '2', '2', '3', '2', '2', '3', '3', '2', '3', '4', '4', '3', '5', '4', '5', '3', '4', '4', '5', '4', '3', '4', '3', '5', '4', '4', '3', '3', '5', '4', '4', '3', '4', '4', '3', '3', '3', '3', '3', '4', '5', '4', '6', '5', '5', '6', '5', '5', '6', '6', '6', '6', '6', '6', '5', '6', '8', '9', '9', '6', '8', '7', '6', '7', '8', '11', '10', '8', '12', '10', '9', '9', '10', '10', '8', '9', '10', '11', '12', '13', '11', '12', '11', '9', '10', '14', '9', '11', '8', '8', '9', '9', '8', '9', '10', '7', '8', '8', '8', '9', '10', '9', '9', '11', '12', '12', '11', '12', '12', '12', '12', '11', '9', '11', '10', '10', '9', '11', '10', '10', '9', '9', '9', '10', '10', '10', '9', '9', '7', '8', '7', '8', '8', '11', '11', '10', '13', '11', '11', '12', '10', '11', '9', '17', '11', '12', '13', '15', '19', '25', '23', '27', '28', '29', '36', '36', '38', '33', '36', '30', '35', '35', '35', '30', '36', '33', '34', '34', '34', '33', '32', '36', '33', '43', '37', '33', '37', '35', '35', '36', '40', '34', '34', '30', '29', '32', '33', '34', '31', '31', '34', '36', '29', '25', '23', '21', '22', '23', '23', '22', '24', '23', '23', '26', '31', '28', '31', '35', '35', '32', '33', '27', '30', '32', '32', '32', '31', '34', '34', '43', '48', '53', '62', '63', '70', '62', '65', '67', '63', '60', '57', '62', '51', '54', '56', '58', '56', '58', '60', '71', '61', '55', '57', '57', '59', '50', '50', '44', '40', '37', '37', '36', '42', '40', '39', '40', '35', '38', '38', '36', '45', '47', '100', '69', '51', '47', '46', '48', '45', '41', '44', '42', '43', '44', '37', '41', '42', '42', '38', '36', '38', '40', '34', '34', '33', '36', '38', '36', '35', '36', '30', '34', '34', '29', '28', '26', '22', '19', '19', '47', '36', '24', '27', '27', '28', '27', '27', '21', '18', '18', '16', '14', '14', '14', '13', '12', '13', '15', '12', '12', '13', '11', '11', '10', '13', '11', '10', '11', '11', '10', '10', '10', '8', '9', '9', '8', '7', '7', '7', '7', '6', '7', '7', '8', '6', '7', '6', '6', '6', '5', '5', '6', '5', '5', '5', '6', '6', '6', '5', '6', '6', '6', '6', '6', '6', '6', '5', '6', '6', '6', '6', '5', '6', '7', '6', '6', '6', '6', '6', '6', '5', '6', '6', '6', '5', '5', '5', '6', '5', '5', '5', '5', '5', '5', '5', '6', '5', '5', '4', '4', '5', '4', '4', '4', '4', '4', '4', '4', '5', '4', '5', '5', '4', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', '4', '5', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '3', '3', '4', '4', '4', '3', '3', '3', '3', '3', '3', '4', '3', '3', '3', '4', '3', '3', '3', '3', '4', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', '4', '3', '3', '2', '3', '3', '3', '3', '3', '3', '3', '3', '2']
I'm passing them through the following function:
def line_plot(list1,list2,datelist,labellist1="list1",labellist2="list2"):
"""
Plots a chart based on two different lists
:param list1: list
:param list2: list
:param datelist: a list of dates
:return: a chart with both lists
"""
import matplotlib.pyplot as plt
norm_list1=[float(i)/max(list1) for i in list1]
norm_list2=[float(i)/max(list2) for i in list2]
plt.plot(datelist,norm_list1,label=labellist1)
plt.plot(datelist,norm_list2,label=labellist2)
plt.xlabel("Date")
plt.legend()
plt.show()
I'm getting the following error message:
Traceback (most recent call last):
File "/Users/santanna_santanna/PycharmProjects/APIGTrends/TrendVesting.py", line 670, in <module>
line_plot(list_of_lists[0],list_of_lists[1],datelist)
File "/Users/santanna_santanna/PycharmProjects/APIGTrends/TrendVesting.py", line 587, in line_plot
norm_list1=[float(i)/max(list1) for i in list1]
TypeError: unsupported operand type(s) for /: 'float' and 'str'
Any ideas on what am I doing wrong?
You try to divide an integer by a string.
norm_list1=[float(i)/max(list1) for i in list1]
norm_list2=[float(i)/max(list2) for i in list2]
max(list1) will give the heighest string in the list, since your lists are made of strings. Why do you have strings in the list and not integers? Convert your list to int or float or use the code below
To solve the error and keep your strings (which is probably not what you want):
norm_list1=[float(i)/float(max(list1)) for i in list1]
norm_list2=[float(i)/float(max(list2)) for i in list2]
To just convert your lists to int or float and solve the error (probably what you want):
newlist1 = [float(i) for i in list1]
newlist2 = [float(i) for i in list2]
And use the new lists instead.
max from list of string integers returns also string, so you are trying to divide float and string.
>>> max(['333', '22222'])
'333'
Why? answer
>>> 2.0 / '3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'float' and 'str'

Python - Convert each integer to string in list and add a comma

I am new to python. I need to create a list of integers from 1 to 70, but for each integer I want to make it a string and a comma after it and store it in another list.
Ex:
for i in range (1,71):
list_of_ints.append(i)
{ Some code
}
it should then be something like this
columns = ['1','2','3','4'.......'70']
Use [str(i) for i in range(1, 71)]. This gives you the list of str(i) for all i in range(1, 71). The function str(i) returns i as a str value instead of as an int
Seems like you want something like this,
>>> l = []
>>> for i in range(1,71):
l.append(str(i))
>>> l
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70']
new_list = [str(x) for x in range(1, 71)]
Using a list comprehension to achieve the same result.
You can use map to help you here:
>>> list_of_ints = range(1, 71)
>>> list_of_ints = map(str, list_of_ints)
>>> print list_of_ints
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70']
>>>

Categories

Resources