函数名称 | 说明
import pandas as pd
import numpy as np
import osos.getcwd()'D:\\Jupyter\\notebook\\Python数据清洗实战\\数据清洗之数据转换'os.chdir('D:\\Jupyter\\notebook\\Python数据清洗实战\\数据')df = pd.read_csv('MotorcycleData.csv', encoding='gbk')df.head(5)<div>
<style scoped>
.dataframe tbody tr th:only-of-type { vertical-align: middle;}.dataframe tbody tr th { vertical-align: top;}.dataframe thead th { text-align: right;}</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;"> <th></th> <th>Condition</th> <th>Condition_Desc</th> <th>Price</th> <th>Location</th> <th>Model_Year</th> <th>Mileage</th> <th>Exterior_Color</th> <th>Make</th> <th>Warranty</th> <th>Model</th> <th>...</th> <th>Vehicle_Title</th> <th>OBO</th> <th>Feedback_Perc</th> <th>Watch_Count</th> <th>N_Reviews</th> <th>Seller_Status</th> <th>Vehicle_Tile</th> <th>Auction</th> <th>Buy_Now</th> <th>Bid_Count</th></tr></thead>
<tbody>
<tr> <th>0</th> <td>Used</td> <td>mint!!! very low miles</td> <td>$11,412</td> <td>McHenry, Illinois, United States</td> <td>2013.0</td> <td>16,000</td> <td>Black</td> <td>Harley-Davidson</td> <td>Unspecified</td> <td>Touring</td> <td>...</td> <td>NaN</td> <td>FALSE</td> <td>8.1</td> <td>NaN</td> <td>2427</td> <td>Private Seller</td> <td>Clear</td> <td>True</td> <td>FALSE</td> <td>28.0</td></tr><tr> <th>1</th> <td>Used</td> <td>Perfect condition</td> <td>$17,200</td> <td>Fort Recovery, Ohio, United States</td> <td>2016.0</td> <td>60</td> <td>Black</td> <td>Harley-Davidson</td> <td>Vehicle has an existing warranty</td> <td>Touring</td> <td>...</td> <td>NaN</td> <td>FALSE</td> <td>100</td> <td>17</td> <td>657</td> <td>Private Seller</td> <td>Clear</td> <td>True</td> <td>TRUE</td> <td>0.0</td></tr><tr> <th>2</th> <td>Used</td> <td>NaN</td> <td>$3,872</td> <td>Chicago, Illinois, United States</td> <td>1970.0</td> <td>25,763</td> <td>Silver/Blue</td> <td>BMW</td> <td>Vehicle does NOT have an existing warranty</td> <td>R-Series</td> <td>...</td> <td>NaN</td> <td>FALSE</td> <td>100</td> <td>NaN</td> <td>136</td> <td>NaN</td> <td>Clear</td> <td>True</td> <td>FALSE</td> <td>26.0</td></tr><tr> <th>3</th> <td>Used</td> <td>CLEAN TITLE READY TO RIDE HOME</td> <td>$6,575</td> <td>Green Bay, Wisconsin, United States</td> <td>2009.0</td> <td>33,142</td> <td>Red</td> <td>Harley-Davidson</td> <td>NaN</td> <td>Touring</td> <td>...</td> <td>NaN</td> <td>FALSE</td> <td>100</td> <td>NaN</td> <td>2920</td> <td>Dealer</td> <td>Clear</td> <td>True</td> <td>FALSE</td> <td>11.0</td></tr><tr> <th>4</th> <td>Used</td> <td>NaN</td> <td>$10,000</td> <td>West Bend, Wisconsin, United States</td> <td>2012.0</td> <td>17,800</td> <td>Blue</td> <td>Harley-Davidson</td> <td>NO WARRANTY</td> <td>Touring</td> <td>...</td> <td>NaN</td> <td>FALSE</td> <td>100</td> <td>13</td> <td>271</td> <td>OWNER</td> <td>Clear</td> <td>True</td> <td>TRUE</td> <td>0.0</td></tr></tbody>
</table>
<p>5 rows × 22 columns</p>
</div>
df.info()<class 'pandas.core.frame.DataFrame'>RangeIndex: 7493 entries, 0 to 7492Data columns (total 22 columns):Condition 7493 non-null objectCondition_Desc 1656 non-null objectPrice 7493 non-null objectLocation 7491 non-null objectModel_Year 7489 non-null float64Mileage 7468 non-null objectExterior_Color 6778 non-null objectMake 7489 non-null objectWarranty 5109 non-null objectModel 7370 non-null objectSub_Model 2426 non-null objectType 6011 non-null objectVehicle_Title 268 non-null objectOBO 7427 non-null objectFeedback_Perc 6611 non-null objectWatch_Count 3517 non-null objectN_Reviews 7487 non-null objectSeller_Status 6868 non-null objectVehicle_Tile 7439 non-null objectAuction 7476 non-null objectBuy_Now 7256 non-null objectBid_Count 2190 non-null float64dtypes: float64(2), object(20)memory usage: 1.3+ MB# 里面有字符串,不能进行转换
# df['Price'].astype(float)# .str 方法可用于提取字符
df['Price'].str[1:3].head(5)0 111 172 3,3 6,4 10Name: Price, dtype: object# 首先要对字符串进行相关处理
df['价格'] = df['Price'].str.strip('$')df['价格'].head(5)0 11,412 1 17,200 2 3,872 3 6,575 4 10,000 Name: 价格, dtype: objectdf['价格'] = df['价格'].str.replace(',', '')df['价格'].head(5)0 11412 1 17200 2 3872 3 6575 4 10000 Name: 价格, dtype: objectdf['价格'] = df['价格'].astype(float)df['价格'].head(5)0 11412.01 17200.02 3872.03 6575.04 10000.0Name: 价格, dtype: float64df.dtypesCondition objectCondition_Desc objectPrice objectLocation objectModel_Year float64Mileage objectExterior_Color objectMake objectWarranty objectModel objectSub_Model objectType objectVehicle_Title objectOBO objectFeedback_Perc objectWatch_Count objectN_Reviews objectSeller_Status objectVehicle_Tile objectAuction objectBuy_Now objectBid_Count float64价格 float64dtype: object# 字符串分割
df['Location'].str.split(',').str[0].head(5)0 McHenry1 Fort Recovery2 Chicago3 Green Bay4 West BendName: Location, dtype: object# 计算字符串的长度
df['Location'].str.len().head(5)0 32.01 34.02 32.03 35.04 35.0Name: Location, dtype: float64原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。