Pandas library 02_DataFrame data structure

#DataFrame data structure, much like a two-dimensional table data structure, python is the most commonly used data structures

import pandas as pd
import numpy as np

# Create DataFrame data
# first give a dictionary data, we use the dictionary to create
the Data = {
"name": [ "Don Ho", "Wang", "Pharaoh", "Zhao Three", "John Doe"] ,
"Sex": [ "male", "female", "male", "female", "man"],
"year": [37,22,15,18,33],
"City": [ "Chengdu "" Beijing "," Shanghai "," Chengdu "," Shenzhen "]
}
# DF1 = pd.DataFrame (the Data)
# Print (DF1)
" ""
name Sex year City
0 Chengdu Tang Hao Male 37
1 female 22 Beijing Wang
2 Pharaoh Male 15 Shanghai
3 Zhao Chengdu and three women 18
4 33 John Doe male Shenzhen
"" "

# df2=pd.DataFrame(data,columns=["name","year","sex","city"]) #指定列名顺序
# print(df2)

Sequence index specified column #
# df3 = pd.DataFrame (data, columns = [ "name", "year", "sex", "city"], index = [ "a", "b", "c", "d", "e"] ) # column names order
# print (df3)

# The above is demonstrated data dictionary creation DataFrame
# below to create DataFrame data from other data types

# Numpy matrix data to create DataFrame data pd, the column is not specified, then the column names with the same index,
# NP1 = np.arange (0,12) .reshape (4,3)
# Print (NP1)
# DF4 = pd.DataFrame (NPl)
# Print (DF4)
# = DF5 pd.DataFrame (NPl, columns = [ "a", "two", "three"]) column names #
# print (df5)

# With Series data pd to create DataFrame, is also possible, but only one data due Series is one-dimensional
# objs1 = pd.Series ([ "name ", "year", "sex", "city"])
Print # (objs1)
# = DF6 pd.DataFrame (objs1)
# Print (DF6)

# Created by tuples DataFrame data, the same results with the results of pd.Series, because all one-dimensional array
# LL = [ "namel", "yearl", "sexl", "cityl"]
# tt = ( " namet "," yeart "," sext "," cityt ")
# = df7l pd.DataFrame (LL)
# = df8t pd.DataFrame (TT)
# Print (df7l)
# Print (" _________ ")
# Print (df8t)

# Set the name attribute, Note: The column can be accessed via DF9 [ "Column Name"], can also be accessed by the column name DF9 provided.
# DF9 = pd.DataFrame (Data)
# Print (DF9)
# = df9.index.name "the above mentioned id"
# df9.columns.name = "gogo"
# Print ( "________________")
# Print (DF9)
# Print (df9.values)
# Print (df9.keys ())

Guess you like

Origin www.cnblogs.com/yiyea/p/11441789.html