Pandas library data structure _ calculation 05_DataFrame

PD PANDAS AS Import
Import numpy NP AS
# Data = {
# "name": [ "Don Ho", "Wang", "Wang", "three Zhao," "John Doe"],
# "Sex": [ "male", "female", "male", "female", "man"],
# "year": [37,22,15,18,33],
# "City": [ "Chengdu", "Beijing "," Shanghai "," Chengdu "," Shenzhen "]
#}

#Series data, if the same operation is performed on the index, it is not introduced NaN Default
# objs1 = pd.Series ([1,2,3,4,5,6] , index = [ "a", " B "," C "," D "," E "," F "])
# objs2 pd.Series = ([1,1,1,1,1,1], index = [" A "," B "," E "," F "," O "," P "])
# Print (objs1 + objs2) #index let the same value +, not the same is added, but the value of NaN

#DataFrame data, the same, the same index position before adding
# df1 = pd.DataFrame (np.arange (0,16 ) .reshape (4,4), index = [1,2,3,4], columns = [ "A", "B", "C", "D"])
# DF2 = pd.DataFrame (np.arange (0,16) .reshape (4,4 &), index = [2,8,5,4 ], Columns = [ "A", "E", "C", "P"])
# Print (DF1 + DF2)

# Will function with the incoming involved in the processing and calculation, is this high level of stock "" " "
""
For some complex calculations and processing, we need to define a function to handle him,
generally there are three ways:
the Map function, installed function apply in Series each element of the
apply function: a function applied to the rows and columns of DataFrame
applymap function: to apply to each of the element DataFrame
"" "
# Let us look at examples of the basic operation
# fruit = {" name ": [ "apple," "banana", "orange", "watermelon"], "price": [ "5.5 dollars", "1.5 yuan", "3.3 yuan", "2.0 yuan"]}
# = df_fruit pd.DataFrame ( Fruit)
# Print (df_fruit)

DEF func1 # (x):
# return x.split ( "Yuan") [0] # sentence is x function are separated by the element, and then taken back to the first element func1
# separate data may do the following respective operation
# df_fruit [ ". price"] = df_fruit [ ". price"] Map (func1).
# df_fruit [ ". price"] = df_fruit [ ". price"] Map (the lambda X:. x.split ( "yuan") [ 0]) by anonymous function do #
# df_fruit [ "price"] = df_fruit [ "price"] map (lambda x: x [: -. 1]) # do with anonymous function
# df_fruit [ "price" ] = df_fruit [ "price"] map (lambda x: ".% 4f"% float (x)). # formats the data
# print (df_fruit)

= pd.DataFrame df44 (np.arange (0,16) .reshape (4,4 &))
Print (df44)
# df45 = df44.applymap (the lambda X:. ". 2F%" X%)
df45 = df44.applymap ( lambda x: x * x) # is applied to each element of the
print (df45)

# Apply here, the example is not, it ranks manipulation stage

# Here simply Kazakhstan Sort:
# Press the index tabs row sort_index (ascending = False) Default Ascending
# column value by row sort_values (by = "column name", ascending = False) ascending = False Descending

Guess you like

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