모두에게 인사합니다. 열 데이터 유형에 따라 NaN 값을 정리하고 채우는 데 필요한 엑셀 파일이 있습니다. 열 데이터 유형이 객체 인 경우 해당 열에 "NULL"을 채워야하고 데이터 유형이 정수이거나 부동 소수점 0을 채워야하는 경우 그 열에서.
지금까지 두 가지 방법을 시도했지만 운이 좋지 않습니다.
df = pd.read_excel("myExcel_files.xlsx")
df.select_dtypes(include='int64').fillna(0, inplace=True)
df.select_dtypes(include='float64').fillna(0.0, inplace=True)
df.select_dtypes(include='object').fillna("NULL", inplace=True)
그리고 내가 얻는 출력은 오류가 아니라 경고 이며 데이터 프레임에 변화가 없습니다.
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py:4259: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
**kwargs
df = pd.read_excel("myExcel_files.xlsx")
#get the list of all integer columns
int_cols = list(df.select_dtypes('int64').columns)
#get the list of all float columns
float_cols = list(df.select_dtypes('float64').columns)
#get the list of all object columns
object_cols = list(df.select_dtypes('object').columns)
#looping through if each column to fillna
for i in int_cols:
df[i].fillna(0,inplace=True)
for f in float_cols:
df[f].fillna(0,inplace=True)
for o in object_cols:
df[o].fillna("NULL",inplace=True)
두 방법 모두 작동하지 않습니다. 도움이나 제안에 감사드립니다. 감사합니다- 매니쉬
select_dtypes
열을 사용 하고 반복 하는 대신 .dtypes
DF를 가져 와서 float64의 wth 0.0과 객체를 "NULL"로 대체 할 수 있다고 생각합니다. 일반적으로 int64에는 누락 된 값이 없기 때문에 걱정할 필요가 없습니다. 채우기 (사용중인 경우 pd.NA
또는 nullable int 유형 이 아닌 경우 )이므로 다음과 같은 단일 작업을 수행 할 수 있습니다.
df.fillna(df.dtypes.replace({'float64': 0.0, 'O': 'NULL'}), inplace=True)
열에 s downcast='infer'
가 될 수있는 것이 있으면 int64
s로 float64
끝나 도록 추가 할 수도 있습니다 int64
. 예를 들면 다음과 같습니다.
df = pd.DataFrame({
'a': [1.0, 2, np.nan, 4],
'b': [np.nan, 'hello', np.nan, 'blah'],
'c': [1.1, 1.2, 1.3, np.nan]
})
그때:
df.fillna(df.dtypes.replace({'float64': 0.0, 'O': 'NULL'}), downcast='infer', inplace=True)
당신에게 줄 것입니다 (참고 열 a
은 int로 다운 캐스트되었지만 c
부동 상태로 유지됩니다) :
a b c
0 1 NULL 1.1
1 2 hello 1.2
2 0 NULL 1.3
3 4 blah 0.0
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다