csv 파일이 있습니다.
Wed Dec 04 11:30:04 GMT+05:30 2019,20,35.0,143455434,0
Wed Dec 04 11:30:13 GMT+05:30 2019,40,25.5,null,
이것을 pandas에로드하고 개별 열을 각 데이터 유형으로 변환하고 싶습니다. 이것이 내가하는 방법
raw_df = pd.read_csv('raw.csv', dtype=str)
raw_df = raw_df.replace({'null':None, pd.np.nan: None})
이것은 변환하는 내 기능입니다.
def df_function(row):
row['timestamp'] = parse(row['timestamp'])
row['odometer'] = float(row['odometer']) + 1
row['speed'] = float(row['speed'])
if row['id'] is not None:
row['id'] = str(row['id'])
if row['error_code'] is not None:
row['error_code'] = int(row['error_code'])
return row
raw_df = raw_df.apply(df_function, axis=1)
열의 데이터 유형을 인쇄하면
timestamp datetime64[ns, tzoffset(None, -19800)]
odometer float64
speed float64
id object
error_code float64
dtype: object
error_code 는 float64이지만 int64 여야하지만 여기서 문제는 무엇입니까?
Integer NA 지원은 현재 전통적인 int8과 비교하여 Int8과 같이 대문자로 표시된 dtype 버전을 사용합니다. 이것은 나중에 변경 될 수 있습니다.
열을 다음으로 변경해야합니다. Int8
df = pd.DataFrame({"error_code":[1,2,5,np.nan]})
print(df.dtypes)
# error_code float64
# dtype: object
df["error_code"] = df["error_code"].astype("Int8")
print(df.dtypes)
산출:
error_code Int8
dtype: object
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다