[英]How to solve pandas.get_dummies Exception: Data must be 1-dimensional
[英]get_dummies(), Exception: Data must be 1-dimensional
我有这些数据
我想申请这个:
one_hot = pd.get_dummies(df)
但我得到这个错误:
这是我的代码,直到那时:
# Import modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import tree
df = pd.read_csv('AllMSAData.csv')
df.head()
corr_matrix = df.corr()
corr_matrix
df.describe()
# Get featurs and targets
labels = np.array(df['CurAV'])
# Remove the labels from the features
# axis 1 refers to the columns
df = df.drop('CurAV', axis = 1)
# Saving feature names for later use
feature_list = list(df.columns)
# Convert to numpy array
df = np.array(df)
海事组织, 文件应被更新,因为它说pd.get_dummies
接受的数据是阵列状,和一个2 d numpy
阵列阵列像(尽管有阵列状的没有正式定义 )。 但是,它似乎不喜欢多维数组。
拿这个小例子:
>>> df
a b c
0 a 1 d
1 b 2 e
2 c 3 f
你不能在底层2D numpy
数组上得到假人:
>>> pd.get_dummies(df.values)
例外:数据必须是1维的
但是你可以在数据框本身上找到假人:
>>> pd.get_dummies(df)
b a_a a_b a_c c_d c_e c_f
0 1 1 0 0 1 0 0
1 2 0 1 0 0 1 0
2 3 0 0 1 0 0 1
或者在单个列下面的1D阵列上:
>>> pd.get_dummies(df['a'].values)
a b c
0 1 0 0
1 0 1 0
2 0 0 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.