选择,排序和重命名熊猫中的列

问题描述 投票:0回答:1

我正在尝试找出与熊猫等效的R's select函数。有一个link作为基础知识,但没有提供我想要做的指南!

raw_data = {'patient': [1, 1, 1, 2, 2],
        'obs': [1, 2, 3, 1, 2],
        'treatment': [0, 1, 0, 1, 0],
        'score': ['strong', 'weak', 'normal', 'weak', 'strong']}
df = pd.DataFrame(raw_data, columns = ['patient', 'obs', 'treatment', 'score'])


df.rename(columns = {'treatment':'treat'},inplace=True)


df = df.loc[:, ['treat','score','obs']]

Out[89]: 
   treat   score  obs
0      0  strong    1
1      1    weak    2
2      0  normal    3
3      1    weak    1
4      0  strong    2

我们可以用R's dplyr]做到这一点>

select(df, treat=treatment, score, obs)  that's it. 

我如何只用一行熊猫代码来执行选择,排序和重命名?

我正在尝试找出与R等效的大熊猫选择函数。有一个基本链接,但没有给出我想做的指南! raw_data = {'患者':[1,1,1,2,2],'肥胖':[1,...

python pandas dplyr
1个回答
0
投票

在熊猫中没有一种方法可以选择和重命名,必须像您的解决方案一样使用类似方法:

© www.soinside.com 2019 - 2024. All rights reserved.