zoukankan      html  css  js  c++  java
  • pandas的DataFrame用法

    用来生成DataFrame数据

    1.说明:

    class pandas.DataFrame(data=Noneindex=Nonecolumns=Nonedtype=Nonecopy=False)

    Two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. Can be thought of as a dict-like container for Series objects. The primary pandas data structure.

    Parameters:

    data : numpy ndarray (structured or homogeneous), dict, or DataFrame

    Dict can contain Series, arrays, constants, or list-like objects

    Changed in version 0.23.0: If data is a dict, argument order is maintained for Python 3.6 and later.

    index : Index or array-like

    Index to use for resulting frame. Will default to RangeIndex if no indexing information part of input data and no index provided

    columns : Index or array-like

    Column labels to use for resulting frame. Will default to RangeIndex (0, 1, 2, …, n) if no column labels are provided

    dtype : dtype, default None

    Data type to force. Only a single dtype is allowed. If None, infer

    copy : boolean, default False

    Copy data from inputs. Only affects DataFrame / 2d ndarray input

    代码:

     1 import tensorflow
     2 import lightgbm as lgb
     3 import pandas as pd
     4 import numpy as np
     5 
     6 class Deng(object):
     7     def __init__(self):
     8         pass
     9 
    10     def main(self):
    11         temp = ['a', 'a', 'b', 'c', 'c']
    12         st = pd.Categorical(temp)
    13         print(st)
    14         # [a, a, b, c, c]
    15         # Categories(3, object): [a, b, c]
    16 
    17         # 遍历temp指出temp中每个字符所属类别的位置索引
    18         st2 = st.codes
    19         print(st2)
    20         # [0 0 1 2 2]
    21 
    22     def gen_data(self):
    23         df = pd.DataFrame(data=np.eye(3), columns=['c1', 'c2', 'c3'])
    24         print(df)
    25 
    26 
    27 if __name__ == '__main__':
    28     obj = Deng()
    29     obj.gen_data()

    输出:

        c1   c2   c3
    0  1.0  0.0  0.0
    1  0.0  1.0  0.0
    2  0.0  0.0  1.0
  • 相关阅读:
    Echarts图表 相关技术点
    Jquery off() on() data()
    Js 正则表达式
    Java jar项目获取配置文件的项
    Java String.split 的坑,会忽略空值
    C# 工作流 状态机 门控制
    二维码SDK,高效率,高识别率,甩zxing,zbar几条街
    C#文本转语音,可导出在本地mp3或者wav文件
    api接口签名验证(MD5)
    C# 站点IP访问频率限制 针对单个站点的实现方法
  • 原文地址:https://www.cnblogs.com/demo-deng/p/9614489.html
Copyright © 2011-2022 走看看