如何将 Pandas 列转换为数组并转置它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49545599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:22:55  来源:igfitidea点击:

How to turn a Pandas column into array and transpose it?

pythonarrayspandasnumpytranspose

提问by Stanleyrr

I have a Pandas dataframe called 'training_set' that resembles the screenshot below:

我有一个名为“training_set”的 Pandas 数据框,类似于下面的屏幕截图:

enter image description here

在此处输入图片说明

I try to turn the 'label' column into array and transpose it. I tried doing Y_train=np.asarray(training_set['label'])but what I got is a horizontal array that resembles the screenshot below, which is not what I want.

我尝试将“标签”列转换为数组并将其转置。我尝试过,Y_train=np.asarray(training_set['label'])但我得到的是一个类似于下面屏幕截图的水平数组,这不是我想要的。

enter image description here

在此处输入图片说明

I want the array to display vertically just like the screenshot below (The screenshot has 2 variables per row. My desired output should only contain 1 variable, the 'label', per row.)

我希望数组像下面的屏幕截图一样垂直显示(屏幕截图每行有 2 个变量。我想要的输出应该只包含 1 个变量,即“标签”,每行。)

enter image description here

在此处输入图片说明

Any suggestion or help would be greatly appreciated!

任何建议或帮助将不胜感激!

回答by cs95

pandas >= 0.24

Pandas >= 0.24

Use DataFrame.to_numpy(), the new Right Way to extract a numpy array:

使用DataFrame.to_numpy()新的正确方法来提取 numpy 数组:

training_set[['label']].to_numpy()


pandas < 0.24

Pandas < 0.24

Slice out your column as a single columned DataFrame(using [[...]]), not as a Series:

将您的列切成单列DataFrame(使用[[...]]),而不是作为Series

Y_train = np.asarray(training_set[['label']])

Or,

或者,

Y_train = training_set[['label']].values

回答by sacuL

Another way would be to reshape your array to shape (-1,1), which means "infer number of rows, force to 1 column":

另一种方法是将数组重塑为 shape (-1,1),这意味着“推断行数,强制为 1 列”:

Y_train = np.array(training_set['label']).reshape(-1,1)

回答by jpp

One way:

单程:

Y_train = training_set['label'].values[:, None]