당신이를 설정 DataFrame.ix[]
을 사용할 수 있습니다 데이터를 0으로 설정하십시오.
먼저 더미 DataFrame 만들기 :
N = 10000
df = pd.DataFrame(np.random.rand(N, 12), columns=["h%d" % i for i in range(1, 13)], index=["row%d" % i for i in range(1, N+1)])
df["sourceid"] = np.random.randint(0, 50, N)
df["destid"] = np.random.randint(0, 50, N)
그런 다음 필터 각각에 대해 당신이 호출 할 수 있습니다 : 당신은 600K 행이 있기 때문에
df.ix[df.sourceid == 10, "h4":"h6"] = 0
는, 어쩌면 느린 df.sourceid == 10
에 의해 마스크 배열을 만들 수 있습니다.
sourceid = pd.Series(df.index.values, index=df["sourceid"].values).sort_index()
destid = pd.Series(df.index.values, index=df["destid"].values).sort_index()
다음 sourceid의 == 10 H4, H5, H6 제외 :
df.ix[sourceid[10], "h4":"h6"] = 0
는 행 ID 곳 sourceid를 찾을 수를 당신은 시리즈가 DataFrame의 인덱스에 해당 맵 값 개체를 만들 수 있습니다 == (10, 20), 목적 ID ==
np.intersect1d(sourceid[10].values, destid[20].values, assume_unique=True)
행 ID를 어디서 찾을 10 < = sourceid < = 12 3 = < < 목적 ID = 5 :
np.intersect1d(sourceid.ix[10:12].values, destid.ix[3:5].values, assume_unique=True)
인덱스 값이 순서대로 중복되는 인덱스 값이있는 시리즈 인 경우 sourceand 및 destid는 인덱스를 찾기 위해 searchsorted를 사용합니다. 그것은 O (로그 N)이고, O (N) 인 마스크 배열을 더 빨리 만듭니다.
내보고를하는 데 도움이된다고 생각하지만, 제 배제와 함께 도움이되는지 모르겠습니다. 필자는 600K 행의 데이터가 있다고 언급 했어야하며 데이터 집합을 줄이기 위해 200 개의 필터가 있어야합니다. 나는 다른 질문 [link] (http://stackoverflow.com/questions/11418192/pandas-complex-filter-on-rows-of-dataframe?rq=1)에서 제안 된 답변을 살펴보기 시작했습니다. – Glenn