SoFunction
Updated on 2024-11-17

Python data analysis pandas read data

I. Reading of three data files

在这里插入图片描述

Second, csv, tsv, txt file reading

1) CSV file reading:

Syntax format: pandas.read_csv(file path)
The contents of the CSV file are as follows:

在这里插入图片描述

import pandas as pd
file_path = "e:\\pandas_study\\"
content = pd.read_csv(file_path)

()  # Returns the first 5 rows of data by default
(3)  # Return the first 3 rows of data
  # Returns a tuple (total number of rows, total number of columns), the total number of rows does not include the header rows

    # Returns the index, which is an iterable object <class ''>

    # Return all column names Index(['name', 'age', 'origin'], dtype='object')

  # The return is the data type of each column
name and surname    object
(a person's) age     int64
place of ancestry    object
dtype: object

2)CSV file reading:

Syntax format: pandas.read_csv(file path)
The contents of the CSV file are as follows:

在这里插入图片描述

import pandas as pd
file_path = "e:\\pandas_study\\"

content = pd.read_csv(file_path,sep='\t',header = None ,names= ['name','age','adress'])
# Parameter Description:
# header = None means no header line
# sep='\t' means to remove spaces from the separators
# names= ['name','age','address'] with columns customized to 'name','age','address' in that order

()  # Returns the first 5 rows of data by default
(3)  # Return the first 3 rows of data
  # Returns a tuple (total number of rows, total number of columns), the total number of rows does not include the header rows

    # Returns the index, which is an iterable object <class ''>

    # Return all column names Index(['name', 'age', 'origin'], dtype='object')

  # The return is the data type of each column

Third, excel file reading

在这里插入图片描述

import pandas as pd
file_path = "e:\\pandas_study\\"
content = pd.read_excel(file_path)

()  # Returns the first 5 rows of data by default
(3)  # Return the first 3 rows of data
  # Returns a tuple (total number of rows, total number of columns), the total number of rows does not include the header rows

    # Returns the index, which is an iterable object <class ''>

    # Return all column names Index(['name', 'age', 'origin'], dtype='object')

  # The return is the data type of each column
name and surname    object
(a person's) age     int64
place of ancestry    object
dtype: object

IV. Database table reading

Grammar: pandas.read_sql(sql statement, database connection object)
Data objects can be created by connecting to mysql or oracle according to modules such as pymysql,cx_oracle.

to this article on Python data analysis pandas read data article is introduced to this, more related pandas read data content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!