-2
안녕하세요 동료 프로그래머행을 논리적 인덱싱을 사용하여 파이썬 데이터 프레임으로 복사
논리 인덱싱을 사용하여 행을 데이터 프레임으로 복사하려고하는데, 내가 기대
예 어떻게 작동 : 가능하면
# pseudo code: example to create logical vector
logical_vector = df.loc[:,'colname']==x
# pseudo code: example to use logical vector to index a dataframe
df[logical_vector,:]=df1.loc[logical_vector2,:]
이것은 기본 동작 중 하나는, 신속하게 할 수 있다는 생각은 빨리 논리적 색인을 사용하여 dataframe의 행 번호를 대체하는 것입니다 행렬 연산. 루프를 제거하지 않고이를 해결하는 방법은 무엇입니까?
:# Use logical indexes x.loc[:,'t']==0 and x.loc[:,'t']==1 to point and get
# data into x. This should replace all row values that contain '0' in column
# t with row values from columns that have '1' for column t
x.loc[x.loc[:,'t']==0,:]=x.loc[x.loc[:,'t']==1,:]
x
Out[78]:
ac bc cc dc ec t
a -1.029517 1.936904 1.143655 0.708996 -1.218484 1.0
b NaN NaN NaN NaN NaN NaN
c 2.369828 0.559880 -0.878904 0.673454 -0.630927 1.0
d NaN NaN NaN NaN NaN NaN
e 1.500134 0.534379 0.375362 0.849761 -1.675824 1.0
f NaN NaN NaN NaN NaN NaN
g -1.359863 0.433721 -0.625973 -0.477530 -0.542612 1.0
h NaN NaN NaN NaN NaN NaN
i 1.357809 -0.017611 0.539137 -1.016894 0.172672 1.0
j NaN NaN NaN NaN NaN NaN
내가이 기대 동안 :
# create example 10x5 dataframe containing random numbers
x=pd.DataFrame(np.random.randn(10,5),columns=['ac', 'bc','cc','dc',
'ec'],index = ['a','b','c','d','e','f','g','h','i','j'])
# add a column containing information for use in example of logical indexing
x['t']= [1,0,1,0,1,0,1,0,1,0]
x
Out[76]:
ac bc cc dc ec t
a -1.029517 1.936904 1.143655 0.708996 -1.218484 1
b 1.836638 -0.723243 -0.501546 -2.046355 0.248156 0
c 2.369828 0.559880 -0.878904 0.673454 -0.630927 1
d -0.629210 1.261608 -0.190508 -0.582700 0.068166 0
e 1.500134 0.534379 0.375362 0.849761 -1.675824 1
f 1.399520 0.038366 -0.137986 0.156580 -0.674619 0
g -1.359863 0.433721 -0.625973 -0.477530 -0.542612 1
h -0.694573 -0.196907 -0.372210 0.464188 -1.217399 0
i 1.357809 -0.017611 0.539137 -1.016894 0.172672 1
j 0.366195 0.750404 -0.055895 0.358795 0.181593 0
그럼 내가이 얻을 색인을 사용하여 대체하려고 :
나는 문제가 무엇인지 보여주기 위해 예를 만들었습니다
Out[76]:
ac bc cc dc ec t
a -1.029517 1.936904 1.143655 0.708996 -1.218484 1
b -1.029517 1.936904 1.143655 0.708996 -1.218484 1
c 2.369828 0.559880 -0.878904 0.673454 -0.630927 1
d 2.369828 0.559880 -0.878904 0.673454 -0.630927 1
e 1.500134 0.534379 0.375362 0.849761 -1.675824 1
f 1.500134 0.534379 0.375362 0.849761 -1.675824 1
g -1.359863 0.433721 -0.625973 -0.477530 -0.542612 1
h -1.359863 0.433721 -0.625973 -0.477530 -0.542612 1
i 1.357809 -0.017611 0.539137 -1.016894 0.172672 1
j 1.357809 -0.017611 0.539137 -1.016894 0.172672 1
내가 뭔가를 놓쳤습니까?
안녕 법학이 내 질문을 해결, 감사합니다! – Alex