本文翻译自:Selecting multiple columns in a pandas dataframe
I have data in different columns but I don't know how to extract it to save it in another variable. 我在不同的列中有数据,但是我不知道如何提取数据以将其保存在另一个变量中。
index a b c
1 2 3 4
2 3 4 5
How do I select 'a'
, 'b'
and save it in to df1? 如何选择'a'
, 'b'
并将其保存到df1?
I tried 我试过了
df1 = df['a':'b']
df1 = df.ix[:, 'a':'b']
None seem to work. 似乎没有任何工作。
#1楼
参考:https://stackoom.com/question/lLu1/在pandas数据框中选择多个列
#2楼
The column names (which are strings) cannot be sliced in the manner you tried. 列名(字符串)无法按照您尝试的方式进行切片。
Here you have a couple of options. 在这里,您有两个选择。 If you know from context which variables you want to slice out, you can just return a view of only those columns by passing a list into the __getitem__
syntax (the []'s). 如果您从上下文中知道要切出哪些变量,则可以通过将列表传递给__getitem__
语法([])来仅返回那些列的视图。 <