Pandas学习笔记01

Pandas python的一个库,主要用于数据分析

  1. 基础:https://www.cnblogs.com/HusterX/p/14673631.html
  2. 清理:https://www.cnblogs.com/HusterX/p/14673920.html

本节主要介绍内容:

DataFrame,Series,ReadCsv,ReadJson

# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
import pandas as pd

mydataset = {
    'cars' : ["BMW","Volvo","Ford"],
    'passings' : [3,7,2],
    'price' : [1,2,3]
}
# DataFrame is the whole table (like a table with rows and columns)
# load data into a DataFrame object
myvar = pd.DataFrame(mydataset)
# Pandas use the loc attribute to return on or more specified row(s)
# this example returns a pandas Series
print(myvar.loc[0])
print("01---")
# return row 0 and 1 (use a list of indexes), the result is a Pandas DataFrame
print(myvar.loc[ [0, 1] ])
print("02---")
# Add a list of names to give each row a name
myvar = pd.DataFrame(mydataset, index = ["BMW", "Volvo", "Ford"])
print(myvar.loc["Ford"])
print("03---")
# Load files into a DataFrame
myvar = pd.read_csv("https://www.w3schools.com/python/pandas/data.csv")
# By default, when you print a DataFrame, you will only get the first 5 rows, and the last 5 rows
# use to_string() to print the entire DataFrame
print(myvar.to_string())
print("04---")
# The head() method returns the headers and a specified number of rows, starting from top. If the number of rows is not specified, the head() method will return the top rows. There is also a tail() method for viewing the last rows of the DatFrame. The tail() method returns the headers and specified number of rows, starting from the bottom.
# Get a quick overview by pringting the first 10 rows of the DataFrame:
print(myvar.head(10))
print("05---")
# The method called info(), that gives you more information about the data set.
print(myvar.info())
print("06---")
# Load the json file into a DataFrame
myvar = pd.read_json("https://www.w3schools.com/python/pandas/data.js");
print(myvar.to_string())
print("---")
# JSON = Python Dictionary (JSON objects have the same format as Python dictionaries). If your JSON code is not in a fiel, but in a python Dictionary, you can load it into a DataFrame directly.
# Series is like a column 
arr = [1,2,3]
# With the index argument , you can name your own labels
myvar = pd.Series(arr, index = ["x", "y", "z"])
# print(myvar["y"])
calories = {"day01":420, "day02":380, "day03":390}
# To select only some of the item in the dictionary ,use the index argumen and specift only the items you want to include in the Series
myvar = pd.Series(calories, index = ["day01", "day02"])

print(myvar)

参考:https://www.w3schools.com/python/pandas/default.asp
在线工具:https://www.programiz.com/python-programming/online-compiler/

posted @ 2021-04-18 15:42  徐春晖  阅读(65)  评论(0编辑  收藏  举报