Pandas 使用笔记
创建空的数据框:
import pandas as pd df = pd.DataFrame(columns = ["ebayno", "p_sku", "sale", "sku"]) #创建一个空的dataframe
数据框拼接,使用 append():
dfDetailAll =pd.DataFrame(columns=["ts_code", "name", "in_date", "out_date"]) # 创建一个空的datafra for index, row in df.iterrows(): print("index="+str(index)) code = row['code'] dfTemp = pro.concept_detail(id=code, fields='ts_code,name,in_date, out_date') # in_date, out_date # print(str(len(dfTemp))) print(dfTemp.head()) dfDetailAll = dfDetailAll.append(dfTemp)
查看数据数据框列类型:
print(df.dtypes) print(df.head()) ts_code object trade_date datetime64[ns] close float64 open float64 high float64 low float64 pre_close float64 change float64 pct_chg float64 vol float64 amount float64
数据框中的某日期字符串转换成日期类型:
df['trade_date'] = pd.to_datetime(df['trade_date'])