#pandas基本应用
import pandas as pd
#两种数据结构,Series和DataFrame
#Series
df1=pd.Series([1,2,3,4,5],index=("a","b","c","d","e"))
print(df1)
# a 1
# b 2
# c 3
# d 4
# e 5
# dtype: int64
#DataFrame
df2=pd.DataFrame([[1,2,3,4],[11,22,33,44],[111,222,333,444],[1111,2222,3333,4444]],
columns=["甲","乙","丙","丁"])
print(df2)
# 甲 乙 丙 丁
# 0 1 2 3 4
# 1 11 22 33 44
# 2 111 222 333 444
# 3 1111 2222 3333 4444
#获取指定单行
print(df2.loc[3])
# 甲 1111
# 乙 2222
# 丙 3333
# 丁 4444
# Name: 3, dtype: int64
#获取指定连续行
print(df2.loc[1:3])
# 甲 乙 丙 丁
# 1 11 22 33 44
# 2 111 222 333 444
# 3 1111 2222 3333 4444
#获取指定不连续多行
print(df2.loc[[1,3]])
# 甲 乙 丙 丁
# 1 11 22 33 44
# 3 1111 2222 3333 4444
#获取指定行指定列
print(df2.loc[1,"丙"])
#33
#获取指定连续多行多列
print(df2.loc[0:2,"乙":"丁"])
# 乙 丙 丁
# 0 2 3 4
# 1 22 33 44
# 2 222 333 444
#获取指定不连续列
print(df2.loc[:,["甲","丙"]])
# 甲 丙
# 0 1 3
# 1 11 33
# 2 111 333
# 3 1111 3333
#获取指定连续列
print(df2.loc[:,"甲":"丙"])
# 甲 乙 丙
# 0 1 2 3
# 1 11 22 33
# 2 111 222 333
# 3 1111 2222 3333
#添加行数据append
df3=pd.DataFrame([[11,22,33,44]],columns=["甲","乙","丙","丁"])
print(df3)
df4=df2.append(df3,ignore_index=True)
print(df4)
# 甲 乙 丙 丁
# 0 1 2 3 4
# 1 11 22 33 44
# 2 111 222 333 444
# 3 1111 2222 3333 4444
# 0 11 22 33 44
#添加列数据
df4["申"]=["A","B","C","D","E"]
print(df4)
#去重drop_duplicates
del df4["申"]
df5=df4.drop_duplicates()
print(df5)
# 甲 乙 丙 丁
# 0 1 2 3 4
# 1 11 22 33 44
# 2 111 222 333 444
# 3 1111 2222 3333 4444
#保存成csv文件,没有行号
df5.to_csv("df5.csv",index=False)
df5.to_excel("df5.xlsx",sheet_name="df5",index=False)