Pandas:按整数选择数据框中的多列
Pandas: selecting multiple columns in dataframe by integer
假设我有这个数据框:
df = pd.DataFrame({'a' : (1, 2, 3),
'b' : (1, 2, 3),
'c' : ("one", "two", "three"),
'd' : (4, 5, 6),
'e' : (4, 5, 6),
'f' : (7, 8, 9),
'g' : (7, 8, 9),
'h' : (7, 8, 9)})
我正在尝试 select 第一、第三和第五列,直到最后一列。期望的输出将是:
a c e f g h
0 1 one 4 7 7 8
1 2 two 5 8 7 8
2 3 three 6 9 9 9
如何使用整数 select 多个不连续的列?我尝试了以下方法:
df.iloc[,[0, 3, 5:]]
df.loc[,[0, 3, 5:]]
df.iloc[,[0, 3, 5:len(df.columns)]]
df.loc[,[0, 3, 5:len(df.columns)]]
df.iloc[,[0 + 3 + 5:]]
df.loc[,[0 + 3 + 5:]]
df.iloc[,[0 + 3 + 5:len(df.columns)]]
df.loc[,[0 + 3 + 5:len(df.columns)]]
None 工作
请指教
对连接切片器使用 np.r_
,python 从 0
开始计数,因此对于第三列需要 2
和从 5th
开始的列需要 4:
:
df = df.iloc[:, np.r_[0, 2, 4:len(df.columns)]]
print (df)
a c e f g h
0 1 one 4 7 7 7
1 2 two 5 8 8 8
2 3 three 6 9 9 9
您也可以通过将列表列表与您喜欢的 any methods 拼合来实现。
from pandas.core.common import flatten
df1 = df.iloc[:, list(flatten([[0, 2], range(4, len(df.columns))]))]
df2 = df.iloc[:, np.concatenate([[0, 2], range(4, len(df.columns))])]
假设我有这个数据框:
df = pd.DataFrame({'a' : (1, 2, 3),
'b' : (1, 2, 3),
'c' : ("one", "two", "three"),
'd' : (4, 5, 6),
'e' : (4, 5, 6),
'f' : (7, 8, 9),
'g' : (7, 8, 9),
'h' : (7, 8, 9)})
我正在尝试 select 第一、第三和第五列,直到最后一列。期望的输出将是:
a c e f g h
0 1 one 4 7 7 8
1 2 two 5 8 7 8
2 3 three 6 9 9 9
如何使用整数 select 多个不连续的列?我尝试了以下方法:
df.iloc[,[0, 3, 5:]]
df.loc[,[0, 3, 5:]]
df.iloc[,[0, 3, 5:len(df.columns)]]
df.loc[,[0, 3, 5:len(df.columns)]]
df.iloc[,[0 + 3 + 5:]]
df.loc[,[0 + 3 + 5:]]
df.iloc[,[0 + 3 + 5:len(df.columns)]]
df.loc[,[0 + 3 + 5:len(df.columns)]]
None 工作
请指教
对连接切片器使用 np.r_
,python 从 0
开始计数,因此对于第三列需要 2
和从 5th
开始的列需要 4:
:
df = df.iloc[:, np.r_[0, 2, 4:len(df.columns)]]
print (df)
a c e f g h
0 1 one 4 7 7 7
1 2 two 5 8 8 8
2 3 three 6 9 9 9
您也可以通过将列表列表与您喜欢的 any methods 拼合来实现。
from pandas.core.common import flatten
df1 = df.iloc[:, list(flatten([[0, 2], range(4, len(df.columns))]))]
df2 = df.iloc[:, np.concatenate([[0, 2], range(4, len(df.columns))])]