如何在熊猫中获取数据帧的列切片 - How to take column-slices of dataframe in pandas

问题:

I load some machine learning data from a CSV file.我从 CSV 文件加载了一些机器学习数据。 The first 2 columns are observations and the remaining columns are features.前两列是观测值,其余列是特征。

Currently, I do the following:目前,我执行以下操作:

data = pandas.read_csv('mydata.csv')

which gives something like:这给出了类似的东西:

data = pandas.DataFrame(np.random.rand(10,5), columns = list('abcde'))

I'd like to slice this dataframe in two dataframes: one containing the columns a and b and one containing the columns c , d and e .我想将此数据帧分成两个数据帧:一个包含列ab ,另一个包含列cde

It is not possible to write something like不可能写出类似的东西

observations = data[:'c']
features = data['c':]

I'm not sure what the best method is.我不确定最好的方法是什么。 Do I need a pd.Panel ?我需要一个pd.Panel吗?

By the way, I find dataframe indexing pretty inconsistent: data['a'] is permitted, but data[0] is not.顺便说一下,我发现数据帧索引非常不一致:允许使用data['a'] ,但不允许使用data[0] On the other side, data['a':] is not permitted but data[0:] is.另一方面,不允许使用data['a':]但允许使用data[0:] Is there a practical reason for this?这有实际的原因吗? This is really confusing if columns are indexed by Int, given that data[0] != data[0:1]考虑到data[0] != data[0:1] ,如果列由 Int 索引,这真的很令人困惑


解决方案:

参考一: https://en.stackoom.com/question/ikgT
参考二: https://stackoom.com/question/ikgT
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章