同时选择和重命名列

时间:2019-08-08 17:02:00

标签: python r pandas dplyr

我环顾四周,但找不到解决方案。在R's dplyr中,我们可以在一行代码中选择并重命名列。

select(Com=Commander,Sco=Score)

我正在熊猫中做同样的事情,但是还没有找到可行的解决方案!

假设我们有此示例数据

# Create an example dataframe
data = {'Commander': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'Date': ['2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08'], 
        'Score': [4, 24, 31, 2, 3]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df


           Commander          Date  Score
Cochice        Jason  2012, 02, 08      4
Pima           Molly  2012, 02, 08     24
Santa Cruz      Tina  2012, 02, 08     31
Maricopa        Jake  2012, 02, 08      2
Yuma             Amy  2012, 02, 08      3

并想要选择并重命名Commander和Score列

df[['Com'=='Commander','Sco'=='Score']]
  

ValueError:项目的长度错误2而不是5。

我该怎么做?

提前谢谢!

4 个答案:

答案 0 :(得分:4)

有点晚了,也许您已经发现了这个问题,但是我遇到了同样的问题,这里的答案为我提供了所用解决方案的大部分方法。

“如何添加选择范围”的最短答案是将所选列的列表传递给重命名操作的结果数据框:

df.rename(columns = {"Com" : "Commander", "Sco":"Score"})[['Com', 'Sco']]

              Com  Sco
Cochice     Jason    4
Pima        Molly   24
Santa Cruz   Tina   31
Maricopa     Jake    2
Yuma          Amy    3

但是重写列名有点麻烦,对吧?因此,您可以使用字典来初始化重命名:

selector_d = {'Commander': 'Com', 'Score': 'Sco'}

并将其传递给重命名的选择操作:

df.rename(columns=selector_d)[[*selector_d.values()]]
              Com  Sco
Cochice     Jason    4
Pima        Molly   24
Santa Cruz   Tina   31
Maricopa     Jake    2
Yuma          Amy    3

我的情况接近这一点-我有不想重命名的列,但我确实想选择它们。可以通过在重命名/选择字典中包括这些列,但使用相同的名称来完成此操作。

以下是整个过程,并添加了另一列:

data = {
    'Commander': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
    'Date': ['2012, 02, 08', '2012, 02, 08', '2012, 02, 08',
             '2012, 02, 08', '2012, 02, 08'],
    'Score': [4, 24, 31, 2, 3],
    'Team': ['Green', 'Yellow', 'Green', 'Yellow', 'Yellow'],
}
df = pd.DataFrame(data, index=['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df

           Commander          Date  Score    Team
Cochice        Jason  2012, 02, 08      4   Green
Pima           Molly  2012, 02, 08     24  Yellow
Santa Cruz      Tina  2012, 02, 08     31   Green
Maricopa        Jake  2012, 02, 08      2  Yellow
Yuma             Amy  2012, 02, 08      3  Yellow

selector_d = {'Team': 'Team', 'Commander': 'Com', 'Score': 'Sco'}

df.rename(columns=selector_d)[[*selector_d.values()]]

              Team    Com  Sco
Cochice      Green  Jason    4
Pima        Yellow  Molly   24
Santa Cruz   Green   Tina   31
Maricopa    Yellow   Jake    2
Yuma        Yellow    Amy    3

如您所见,这还允许对最终数据框中的列进行重新排序。

答案 1 :(得分:1)

df.rename(columns = {"presentColumnName" : "NametoWhichYouWantTOChangeTo", "presentColumnName":"NametoWhichYouWantTOChangeTo"}, inplace=True)

例如

  

df.rename(columns = {"Com" : "Commander", "Sco":"Score"}, inplace=True)

答案 2 :(得分:0)

尝试一下:

'nannan' when there is no info in any of the column
2.21nan when there if info in the 'Arrival Price Price (local)' column
4.86nan when there if info in the 'Last Mid' column

答案 3 :(得分:0)

您可以在 Python 中以与在 R 中相同的方式进行操作,使用 datar

>>> from datar.all import tibble, select, f
>>> 
>>> data = {'Commander': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
...         'Date': ['2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08'], 
...         'Score': [4, 24, 31, 2, 3]}
>>> 
>>> df = tibble(**data)
>>> df >> select(Com=f.Commander,Sco=f.Score)
       Com     Sco
  <object> <int64>
0    Jason       4
1    Molly      24
2     Tina      31
3     Jake       2
4      Amy       3

免责声明:我是 datar 软件包的作者。

相关问题