astype()进行转换举例:
- import pandas as pd
-
-
- data = {
- 'user_id': ['1', '2', '3'], # 当前是字符串
- 'location_id': ['1', '2', '3'], # 当前是字符串
- 'duration': ['85.0', '457.0', '1335.0'], # 当前是字符串
- 'start_day': ['204', '205', '206'], # 当前是字符串
- 'end_day': ['204', '206', '207'], # 当前是字符串
- 'start_min': ['110', '197', '657'], # 当前是字符串
- 'end_min': ['195', '654', '552'], # 当前是字符串
- 'weekday': ['4', '5', '6'] # 当前是字符串
- }
-
- df = pd.DataFrame(data)
-
- # 打印原始数据类型
- print("原始数据类型:")
- print(df.dtypes)
- '''
- 原始数据类型:
- user_id object
- location_id object
- duration object
- start_day object
- end_day object
- start_min object
- end_min object
- weekday object
- dtype: object
- ''''
-
-
- # 指定列转换数据类型
- df = df.astype({
- 'user_id': int,
- 'location_id': int,
- 'duration': float,
- 'start_day': int,
- 'end_day': int,
- 'start_min': int,
- 'end_min': int,
- 'weekday': int
- })
-
- # 打印转换后的数据类型
- print("\n转换后的数据类型:")
- print(df.dtypes)
- '''
- 转换后的数据类型:
- user_id int64
- location_id int64
- duration float64
- start_day int64
- end_day int64
- start_min int64
- end_min int64
- weekday int64
- dtype: object
- '''