Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按列表选择列(并删除第一行)_Python_Python 3.x_Pandas_Dataframe_Numpy - Fatal编程技术网

Python 按列表选择列(并删除第一行)

Python 按列表选择列(并删除第一行),python,python-3.x,pandas,dataframe,numpy,Python,Python 3.x,Pandas,Dataframe,Numpy,我有一个从数据库中获取行的脚本。 其中一列包含示例文本: "Technical task from package TRF_DATABASE_YU_869 30-APR-21 TRYSG_6079 TRF_DATABASE_YU_869 Finish_Error 30-APR-21 TRYSG_6082 TRF_DATABASE_YU_869 Finish_Error 30-APR-21 TRYSG_6084 TRF_DATABASE_YU_869 Finish_Error 30-A

我有一个从数据库中获取行的脚本。 其中一列包含示例文本:

"Technical task from package TRF_DATABASE_YU_869

30-APR-21 TRYSG_6079 TRF_DATABASE_YU_869 Finish_Error
30-APR-21 TRYSG_6082 TRF_DATABASE_YU_869 Finish_Error
30-APR-21 TRYSG_6084 TRF_DATABASE_YU_869 Finish_Error
30-APR-21 TRYSG_6087 TRF_DATABASE_YU_869 Finish_Error"
我的剧本:

task_list = []
cursor = connection.cursor()
sql_as_string = sql_file.read()
cursor.execute(sql_as_string)

for row in cursor:
    task_list.append(row)
 
for row in task_list:
    col = row[4]
    print(col)  
我正在寻找一种方法来删除“TRF_包数据库中的技术任务”一行,并将np.array应用于以下内容:

an_array = np.array([[1, 2, 3]])
second_and_third_column = an_array[:, 2]
但是我有一个问题col=row[4]-它是一个元组,所以我不能使用_数组[:,2]。 我想得到最后的结果:

TRF_DATABASE_YU_869
TRF_DATABASE_YU_869
TRF_DATABASE_YU_869
TRF_DATABASE_YU_869

如果要删除第一行,只需从元素1而不是零进行迭代,即:

for i in range(1, len(cursor)):
    task_list.append(cursor[i])

这将附加除第一行之外的所有内容。至于你的另一个问题,你能举一个更好的例子来说明你在说什么吗?

好吧,因为你的输入(示例文本)似乎是一个字符串,所以你可以使用string.find()和string.split()手动解析其中的行,例如:

string = samplestring
stringarray = []
linestoSkip = 2    # Feed in how many lines you want to skip
line=0

while True:
    line += 1
    lineChange = string.find("\n") #finds index of linechange
    row = string[:lineChange]
    if line <= linestoSkip:
        stringarray.append(row.split()) #split() splits the string at all whitespaces

    string = string[linechange+1:] # Remove the row from string for next loop
    if lineChange == -1:
        break # If .find() can't find a matching string, it returns -1 => break
string=samplestring
stringarray=[]
linestoSkip=2#输入要跳过的行数
直线=0
尽管如此:
行+=1
lineChange=string.find(“\n”)#查找lineChange的索引
行=字符串[:换行]
如果线