Pandas tips One

Source: Internet
Author: User
Import Pandas as PD

DF1 = PD. Dataframe ({' col1 ': [0,1], ' col_left ': [' A ', ' B ']}) #按列定义
DF2 = PD. Dataframe ({' col1 ': [1,2,2], ' col_right ': [2,2,2]})

Print (DF1)
# # Col1 Col_left
# #0 0 A
# #1 1 B
Print (DF2)
# # Col1 Col_right
# #0 1 2
# #1 2 2
# #2 2 2

#indicator = True places the merged records in a new column
#根据col1进行合并
res = pd.merge (df1,df2,on = ' col1 ', how = ' outer ', indicator = True)
Print (RES)
# # col1 Col_left col_right _merge
# #0 0 A NaN left_only
# #1 1 B 2.0 both
# #2 2 NaN 2.0 right_only
# #3 2 NaN 2.0 right_only

Name of #自定义indicator column
res = pd.merge (df1,df2,on = ' col1 ', how = ' outer ', indicator = ' indicator_column ')
Print (RES)
# # col1 Col_left col_right Indicator_column
# #0 0 A NaN left_only
# #1 1 B 2.0 both
# #2 2 NaN 2.0 right_only
# #3 2 NaN 2.0 right_only

left = PD. Dataframe ({' A ': [' A0 ', ' A1 ', ' A2 '),
' B ': [' B0 ', ' B1 ', ' B2 ']},
index = [' K0 ', ' K1 ', ' K2 '])
right = PD. Dataframe ({' C ': [' C0 ', ' C2 ', ' C3 '),
' D ': [' D0 ', ' D2 ', ' D3 ']},
index = [' K0 ', ' K2 ', ' K3 '])
Print (left)
# # A B
# #K0 A0 B0
# #K1 A1 B1
# #K2 A2 B2
Print (right)
# # C D
# #K0 C0 D0
# #K2 C2 D2
# #K3 C3 D3

#根据index进行合并, how = ' outer ', and print output
res = Pd.merge (Left,right,left_index = true, Right_index = true, how = ' outer ')
Print (RES)

#根据index进行合并, how = ' inner '
res = Pd.merge (Left,right,left_index = true, Right_index = true, how = ' inner ')
Print (RES)
# # A B C D
# #K0 A0 B0 C0 D0
# #K2 A2 B2 C2 D2

Boys = PD. Dataframe ({' K ': [' K0 ', ' K1 ', ' K2 '], ' age ': [1,2,3]})
Girls = PD. Dataframe ({' K ': [' K0 ', ' K1 ', ' K3 '], ' age ': [4,5,6]})

#使用suffixes后缀解决overlapping重叠的问题
res = Pd.merge (boys,girls,on= ' K ', suffixes = [' _boy ', ' _girl '],how = ' inner ')
Print (RES)
# # K Age_boy age_girl
# #0 K0 1 4
# #1 K1 2 5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#数据的可视化 matplotlib--> plt.show ()
Import Pandas as PD
Import NumPy as NP
Import Matplotlib.pyplot as Plt

#随机生成1000个数据
data = PD. Series (NP.RANDOM.RANDN (1000), index = np.arange (1000))

#为了方便观看效果, accumulating data
Data.cumsum ()

#pandas数据可以直接观看可视化效果
Data.plot ()

Plt.show ()

#生成1000 the Dataframe of *4 and add them up
data = PD. Dataframe (Np.random.randn (1000,4),
index = np.arange (1000),
columns = List (' ABCD ')
)
Data.cumsum ()
Data.plot ()
Plt.show ()

#除了plot, scatter scatter, other bar,hist,box,kde,area,hexbin.
Ax = data.plot.scatter (x = ' A ', y = ' B ', color = ' darkblue ', label = ' Class1 ')
#画在同一个ax上面, select a different data column
Data.plot.scatter (x= ' A ', y = ' C ', color = ' lightgreen ', label = ' Class2 ', ax = ax)
Plt.show ()



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.