Pandas

https://www.runoob.com/pandas/pandas-tutorial.html

 

数据源

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

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Panier