pandas 数据框,只保留一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45739427/
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 04:16:15  来源:igfitidea点击:

Dataframe, keep only one column

pandasdataframe

提问by Baptiste Arnaud

I can't find the pandas function that returns a one column Dataframefrom a multi column DF. I need the exact opposit function of the drop([''])one.

我找不到Dataframe从多列返回一列的 Pandas 函数DF。我需要一个完全相反的功能drop([''])

Any ideas?

有任何想法吗?

回答by Scott Boston

You can use the following notation to return a single column dataframe:

您可以使用以下表示法返回单列数据框:

df = pd.DataFrame(data=np.random.randint(1, 100 ,(10, 5)), columns=list('ABCDE'))

df_out = df[['C']]

Output:

输出:

    C
0  65
1  48
2   1
3  41
4  85
5  55
6  45
7  10
8  44
9  11

Note: df['C'] returns a series. And, you can use the to_framemethod to convert that series into a dataframe. Or use the double brackets, [[]].

注意: df['C'] 返回一个系列。并且,您可以使用该to_frame方法将该系列转换为数据帧。或者使用双括号,[[]]。

回答by Abhaypratap Singh

It is very simple just use the double brace to select it. It will return the result in Data Frame. You can check it by type(df)

非常简单,只需使用双括号来选择它。它将在数据帧中返回结果。你可以通过type(df)

# First create a data frame to check this
column = df[['Risk']]
print(column)
print(type(column))