반응형
열 중 하나에 null 값이있는 데이터 프레임에서 행을 제거하려고합니다. 내가 찾을 수있는 대부분의 도움말은 지금까지 나를 위해 작동하지 않은 NaN 값을 제거하는 것과 관련이 있습니다.
여기에 데이터 프레임을 만들었습니다.
# successfully crated data frame
df1 = ut.get_data(symbols, dates) # column heads are 'SPY', 'BBD'
# can't get rid of row containing null val in column BBD
# tried each of these with the others commented out but always had an
# error or sometimes I was able to get a new column of boolean values
# but i just want to drop the row
df1 = pd.notnull(df1['BBD']) # drops rows with null val, not working
df1 = df1.drop(2010-05-04, axis=0)
df1 = df1[df1.'BBD' != null]
df1 = df1.dropna(subset=['BBD'])
df1 = pd.notnull(df1.BBD)
# I know the date to drop but still wasn't able to drop the row
df1.drop([2015-10-30])
df1.drop(['2015-10-30'])
df1.drop([2015-10-30], axis=0)
df1.drop(['2015-10-30'], axis=0)
with pd.option_context('display.max_row', None):
print(df1)
내 결과는 다음과 같습니다.
누군가가이 행을 어떻게 삭제할 수 있는지 알려주시겠습니까?
나는 판다와 아주 오래 일하지 않았고 한 시간 동안 이것에 갇혀있었습니다. 어떤 조언이라도 대단히 감사하겠습니다.
해결 방법
이 작업을 수행해야합니다.
df = df.dropna(how='any',axis=0)
" 모든 "Null 값이있는 모든 행 (축 = 0)을 지 웁니다.
예 :
#Recreate random DataFrame with Nan values
df = pd.DataFrame(index = pd.date_range('2017-01-01', '2017-01-10', freq='1d'))
# Average speed in miles per hour
df['A'] = np.random.randint(low=198, high=205, size=len(df.index))
df['B'] = np.random.random(size=len(df.index))*2
#Create dummy NaN value on 2 cells
df.iloc[2,1]=None
df.iloc[5,0]=None
print(df)
A B
2017-01-01 203.0 1.175224
2017-01-02 199.0 1.338474
2017-01-03 198.0 NaN
2017-01-04 198.0 0.652318
2017-01-05 199.0 1.577577
2017-01-06 NaN 0.234882
2017-01-07 203.0 1.732908
2017-01-08 204.0 1.473146
2017-01-09 198.0 1.109261
2017-01-10 202.0 1.745309
#Delete row with dummy value
df = df.dropna(how='any',axis=0)
print(df)
A B
2017-01-01 203.0 1.175224
2017-01-02 199.0 1.338474
2017-01-04 198.0 0.652318
2017-01-05 199.0 1.577577
2017-01-07 203.0 1.732908
2017-01-08 204.0 1.473146
2017-01-09 198.0 1.109261
2017-01-10 202.0 1.745309
참조 페이지 https://stackoverflow.com/questions/44548721
반응형
'파이썬' 카테고리의 다른 글
파이썬 Python : ufunc 'add'에 dtype ( 'S21') dtype ( 'S21') dtype ( 'S21') 유형과 일치하는 서명이있는 루프가 포함되지 않았습니다. (0) | 2020.10.18 |
---|---|
파이썬 Python의 기존 파일 앞에 줄 추가 (0) | 2020.10.18 |
파이썬 NumPy 다차원 배열의 i 번째 열에 액세스하는 방법은 무엇입니까? (0) | 2020.10.17 |
파이썬 TensorFlow가 GPU에 액세스하지 못하도록 차단 하시겠습니까? (0) | 2020.10.17 |
파이썬 Beautiful Soup to parse url to get another urls data (0) | 2020.10.17 |
댓글