跳过第一列

时间:2019-08-26 00:32:33

标签: pandas

我希望这是一个简单的问题。基本上,我希望没有第一列的输出相同。

import pandas as pd

ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils','Kings', 
 'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
    'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
    'Year': 
    [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
   'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
    df = pd.DataFrame(ipl_data)
   df.loc[df['Team']=='Riders'].values.tolist()

  Out [1]:
   [['Riders', 1, 2014, 876],
    ['Riders', 2, 2015, 789],
    ['Riders', 2, 2016, 694],
   ['Riders', 2, 2017, 690]]

我希望我的输出是:

     Out [1]:
     [[1, 2014, 876],
     [2, 2015, 789],
     [ 2, 2016, 694],
     [2, 2017, 690]]

3 个答案:

答案 0 :(得分:0)

您可以这样做

df.loc[df['Team']=='Riders', ['Rank', 'Year', 'Points'] ].values.tolist()

或者如果您想选择列而不显式指定列名,

columns = df.columns.values.tolist()[1:]
df.loc[df['Team']=='Riders', columns].values.tolist()

答案 1 :(得分:0)

使用:

df.loc[df.Team == "Riders", df.columns[1:]].to_numpy().tolist()
根据{{​​3}},建议使用

to_numpy()代替values。两者都为您提供了numpy数组,因此您仍然可以使用tolist()

答案 2 :(得分:0)

您可以按位置选择所有列,而不必按DataFrame.iloc

class Bhai {
    public var kam: String = ""
}

let b = Bhai()
b.kam = "something"
let closure = { [b] in
    print("This is \(b.kam)")
}
b.kam = "otherVariable"
closure()
// prints This is otherVariable