简体   繁体   English

矩阵第 0 行第 0 列读取“狗”而不是“目标”

[英]Matrix 0th row 0th column reading "Dog" instead of "Target"

I am trying to append certain strings to a matrix.我正在尝试将某些字符串 append 转换为矩阵。 One that reads "Target" on the 0th row 0th column.一个在第 0 行第 0 列上显示“目标”。 And one that reads "Dog" on the 0th column 1st row downwards to the last row of the matrix.还有一个在第 0 列第 1 行向下读取到矩阵最后一行的“Dog”。

My initial matrix looks like: enter image description here我的初始矩阵如下所示:在此处输入图像描述

I have a small issue with the following program:我对以下程序有一个小问题:

import numpy as np
import pandas as pd

main=pd.read_csv('C:/Users/Jonas/Desktop/testfile/biggertest.csv', header=None)

target_col = ['dog'] * main.shape[0]
main.insert(loc = 0, column = 'target', value = target_col)

This creates a new matrix that looks like this: enter image description here这将创建一个如下所示的新矩阵:在此处输入图像描述

Instead of: enter image description here而不是:在此处输入图像描述

I'm wondering what I need to change to make this happen?我想知道我需要改变什么才能做到这一点?

Cheers.干杯。

You could simply make the following modification.您可以简单地进行以下修改。

import numpy as np
import pandas as pd

main=pd.read_csv('C:/Users/Jonas/Desktop/testfile/biggertest.csv', header=None)

target_col = ['dog'] * main.shape[0]
target_col[0] = 'target'
main.insert(loc = 0, column = -1, value = target_col)

Alternatively,或者,

import numpy as np
import pandas as pd

main=pd.read_csv('C:/Users/Jonas/Desktop/testfile/biggertest.csv', header=None)

main.insert(loc = 0, column = -1, value = 'dog')
main.at[0,-1] = 'target'

If you want the column indices to go from 0 to 4 (instead of -1 to 3), then you can add the following command:如果您希望 go 的列索引从 0 到 4(而不是 -1 到 3),则可以添加以下命令:

main.rename(columns = lambda x:x+1,inplace=True)

Resulting output from all commands:所有命令产生的 output :

        0  1  2  3  4
0  target  5  5  8  9
1     dog  9  0  2  6
2     dog  6  6  4  3
3     dog  3  3  3  3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 所有行都期望第0行 - All the rows expect 0th Row 如何基于第0列中的字符串将嵌套列表(用作矩阵)细分为列表 - How to subdivide a nested list (used as a matrix) into lists based on the string in the 0th column 使用熊猫读取第 0 行 csv 并将其保存到列表中 - using pandas read 0th row of csv and save it into list NLTK wordnet界面中的第0个synset - 0th synset in NLTK wordnet interface BerTopic Model - 可视化忽略第 0 个索引 - BerTopic Model - Visualization ignores 0th index 为什么 GraphView 省略了第 0 个顶点? - Why does a GraphView omit the 0th vertex? python中如何删除未命名的索引行,将第0行替换为索引行进行数据分析 - How to delete Unnamed index row and replace 0th row as index row in python for data analysis 如何在 PySpark dataframe 的第 0 轴上找到 arrays(数组列)的平均值? - How to find the average of arrays (an array column) on 0th axis in a PySpark dataframe? python 用于在第一个元素没有第 0 个元素开始的循环 - python for loops being started at 1st element no 0th element 根据第0个列表的argsort对嵌套列表中的列表进行排序 - Sort lists in a nested list based on the argsort of the 0th list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM