[英]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.