我有一个大型的调查数据集来清理(300列,30000行),而且这些列是混合的。我用Python来对付熊猫和小矮人。我非常喜欢使用Python在学习车轮阶段。
当我通过时,我将Y/N的答案改为1或0。然而,对于likert刻度栏,我担心做同样的事情可能会有风险。是否有人认为,目前是否最好将这些数据保留为NaN?性别是一样的--有一个单独的列用于男性和一个女性,两者都填充1表示是和NaN代表no。
我打算使用Python来进行数据分析/图表(将导入matplotlib & seaborn)。由于这对我来说是新的,我猜想我现在所做的改变以后可能会产生意想不到的后果!
如果你能提供任何指导,我将不胜感激。
提前谢谢。
发布于 2015-03-06 07:39:51
如果没有0,那意味着什么,用一个值(为了方便起见)填充NA是很好的。这完全取决于你的数据。也就是说,300x30k并不是那么大。将其保存为CSV,并在IPython笔记本中进行实验,Pandas可能会在一秒钟内读取它,所以如果你搞砸了什么,就重新加载。
下面是一段快速代码,它可以将任何多列问题集压缩为具有一定数量的单个列:
df = pd.DataFrame({
1: {'agree': 1},
2: {'disagree': 1},
3: {'whatevs': 1},
4: {'whatevs': 1}}).transpose()
df

question_sets = {
'set_1': ['disagree', 'whatevs', 'agree'], # define these lists from 1 to whatever
}
for setname, setcols in question_sets.items():
# plug the NaNs with 0
df[setcols].fillna(0)
# scale each column with 0 or 1 in the question set with an ascending value
for val, col in enumerate(setcols, start=1):
df[col] *= val
# create new column by summing all the question set columns
df[setname] = df[question_set_columns].sum(axis=1)
# delete all the old columns
df.drop(setcols, inplace=True, axis=1)
df

https://stackoverflow.com/questions/28893592
复制相似问题