我的功课有问题。我的目标是要比较第一列第一行和第二列第二行。
我是熊猫新手,所以我尝试在互联网上浏览答案,但可惜没有。我想到了一种方法,将熊猫转换为数组并将第一列存储到第一数组,将第二列存储到第二数组。
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]
然后我弹出first_array的第一个数字是4,然后在最后一个这样的数字中插入一个0
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循环
if first_array < last_array:
print ("===")
但是看来我的逻辑是正确的,但是我期望的输出有一点错误
['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']
===
第一个“ ===” 10不是<9,这让我思考。我错过了什么?预先谢谢你。
这是我的代码的一部分。 print(join_tag)是我为此所需的输出。
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)
预期输出为
['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']
===
答案 0 :(得分:1)
如果我正确理解了您的问题,您想将4与16进行比较,对吧?
如果是,则将数据放回数据框中:
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)))
然后:
df.iloc[0, 0] < df.iloc[1, 1]
会比较4个<16
要全部比较:
for i in range(len(df.columns)-1):
print(df.iloc[0, i] < df.iloc[1, i+1])
答案 1 :(得分:0)
您的数组包含字符串。将它们转换为数字。 在第一种情况下,'10'<'9'为真,因为字符串'10'在字母上小于字符串'9'