pandas 基础
1. 文件读入、写入
df = pd.read_csv(r'C:\Coding\DataAnalyse\joyful-pandas-master\joyful-pandas-master\data\table.csv')
print(df.head())
df_txt = pd.read_table(r"C:\Coding\DataAnalyse\joyful-pandas-master\joyful-pandas-master\data\table.txt")
print(df_txt)
df_excel = pd.read_excel(r"C:\Coding\DataAnalyse\joyful-pandas-master\joyful-pandas-master\data\table.xlsx")
print(df_excel.head())
df.to_csv(r"C:\Coding\DataAnalyse\joyful-pandas-master\joyful-pandas-master\data\new_table.csv")
df.to_csv(r"C:\Coding\DataAnalyse\joyful-pandas-master\joyful-pandas-master\data\new_table.csv",index=False)
2、Series基础
s = pd.Series(np.random.randn(5),index=['a','b','c','d','e'], name="This is the first Series")
print(s)
print(s.name)
print(s.index)
print(s.dtype)
print(s['b'])
print(s.mean)
print([attr for attr in dir(s) if not attr.startswith('_')])
3、DateFrame基础
df = pd.DataFrame({'col1':list('abcde'), 'col2':range(5,10), 'col3':[1.3,2.5,3.6,4.6,5.8]},
index=list('一二三四五'))
print(df)
print(df['col1'])
print(type(df))
print(type(df['col1']))
# df.rename(index={'一':"one", columns ={"col1":"new_col1"}})
tt = df.rename(index={'一':'one'}, columns={'col1':'new_col1'})
print(tt)
print(df.index)
print(df.columns)
print(df.values)
print(df.shape)
print(df.mean())
df1 = pd.DataFrame({'A':[1,2,3]},index=[1,2,3])
df2 = pd.DataFrame({'A':[1,2,3]},index=[3,1,2])
print(df1-df2)