https://www.runoob.com/pandas/pandas-tutorial.html
数据源
- series 类似于一个一维的数组,具有数据和索引
- example: data_series = pd([1, 2, 3], indexname=’A’)
- shape: 维度信息
- reviews.shape
- dataFrame: 类似于一个二维的表格或数据库中的数据表 https://www.runoob.com/pandas/pandas-dataframe.html
- example:
data = [[1, 2], [3, 4]]
df = pd.dataFrame(data, columns=[‘col1’, ‘col2’]) - astype:
- 设置每列的数据类型
df[‘col1’] = df[‘col1’].astype(str)
- 设置每列的数据类型
- loc: 返回指定行的数据, [行标签, 列标签]
- print(df.loc[1, ‘country’])
- print(df.loc[[1, 2]]) 返回第一行,第二行
- df.loc[df[‘age’] > 25]
- iloc: [行号, 列号]
- reviews.iloc[1:3, 0]
- describe()
- reviews.points.describe()
- example:
- CSV https://www.runoob.com/pandas/pandas-csv-file.html
- example:
- df = pd.read_csv(‘data.csv’, sep=‘;’, header=0, names=[‘A’, ‘B’, ‘C’], dtype={‘A’: int, ‘B’: float})
- df.to_csv(‘output.csv’, index=False, header=True, columns=[‘A’, ‘B’])
- df.head(), 方法用于读取前面的 n 行,如果不填参数 n ,默认返回 5 行
- df.tail() 用于读取尾部的 n 行,如果不填参数 n ,默认返回 5 行,空行各个字段的值返回 NaN
- df.info() 返回表格的一些基本信息
- example:
- Excel https://www.runoob.com/pandas/pandas-excel.html
- dtype: 显示type,example, reviews.price.dtype
- astype: 改变type. example: reviews.price.astype(‘str’)
- isnull: 返回空值行
- example:
- reviews.country.isnull()
- df[df[‘price’].isnull()]
- example:
- isin:
reviews.loc[reviews.country.isin(['Italy', 'France'])]
- fillna: 是用来**填补缺失值(NaN)**的核心函数
- example:
- reviews.country.fillna(« Unknown »)
- example:
- replace: 替换值
- example
- reviews.countr.replace(‘fr’, ‘france’)
- example
rename()
用于重命名列名或索引名- reviews.rename(index={0: ‘firstline’, 1: ‘secondline’}, column={‘first_name’, ‘last_name’})
- rename_axis: 是 Pandas 用来重命名 索引轴名称(index name)或列轴名称(columns name) 的方法。它不会改行标签或列标签本身,而是给索引或列的轴起个名字
- example:
- reviews.rename_axis(« wines », axis= ‘rows’).rename_axis(‘fields’, axis=’columns’)
- example:
- concat, join, merge
- concat: combined_products = pd.concat([gaming_products, movie_products])
- join, 要设置成索引 df1.set_index(« MeetID »).join(df2.set_index(« MeetID »))
- merge, 可以不用索引,pd.merge([df1, df2], on=“MeetID »)