以 R 演示为例:
df <- matrix(1:100, nrow = 10, ncol = 10)
df :
> df
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
现在我要删除2:8
行和3:7
列,所以我做了:
> eliminated.rows <- 2:8
> eliminated.cols <- 3:7
> df <- df[-eliminated.rows, -eliminated.cols]
然后我得到了我想要的东西:
> df
[,1] [,2] [,3] [,4] [,5]
[1,] 1 11 71 81 91
[2,] 9 19 79 89 99
[3,] 10 20 80 90 100
问题是:
如何使用Python实现我的目标?
修改
具体来说,如果我得到要删除的行和列的列表,比如eliminated_rows = list(), eliminated_cols = list()
,我希望结果df = df[-eliminated_rows, -eliminated_cols]
与python一起使用。
任何帮助将不胜感激。
答案 0 :(得分:1)
你可以这样做:
df = pd.DataFrame(np.random.randint(0,100,size=(10, 10)), columns=list('ABCDEFGHIJ'))
row_i= df.index.isin(range(1,8))
col_i=df.index.isin(range(2,7))
df.iloc[~row_i,~col_i]
在python中从0开始时要小心索引。
答案 1 :(得分:0)
试试这个(小心索引):
Try this:
matrix = []
for row in range(0,100,10):
column = [i for i in range(row,row+10)]
matrix.append(column)
columns_to_remove = [1,2,8]
rows_to_remove = [4,5,9]
for i in rows_to_remove:
matrix.pop(i)
for remaining_rows in matrix:
for remove_column in matrix:
matrix.pop(matrix[remaining_rows][remove_column])
答案 2 :(得分:0)
检查select data from data frame后,我认为此解决方案可能更容易理解:
E.g。 3 * 3
DataFrame,消除行1,2
,列0,1
:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0, 9, size = (3, 3)))
#print df
eliminated_rows = [1, 2] #rows to eliminated
eliminated_cols = [0, 1] #cols to eliminated
range_rows = range(0, df.shape[0])
remaining_rows = [row for row in range_rows if row not in eliminated_rows]
range_cols = range(0, df.shape[1])
remaining_cols = [col for col in range_cols if col not in eliminated_cols]
df = df.iloc[remaining_rows, remaining_cols]
#print df
原始数据框
3 * 3
:
0 1 2
0 4 3 3
1 2 8 7
2 5 1 7
结果:
2
0 3