1 回答

TA貢獻1880條經驗 獲得超4個贊
對于每個股票代碼,該文件由
dicts
, as組成的列。strings
加載您的文件
pandas.read_excel
。使用 .將類型轉換
str
為類型。dict
ast.literal_eval
將 的每一列轉換
dicts
為一個數據框pandas.json_normalize
。每個數據幀將被添加到
dict
,df_dict
,其中鍵將是股票代碼。將股票代碼作為一列添加到每個數據幀
將所有數據幀組合成一個數據幀,并帶有
pandas.concat
.使用 , 將列轉換
'time'
為日期時間格式pandas.to_datetime
,并設置為索引。使用或訪問該
'close'
列。df.close
df['close']
用于
pandas.DataFrame.pivot
獲取'close'
值,以代碼作為標題和'time'
索引。
import pandas as pd
from ast import literal_eval
# load the file
df = pd.read_excel('Crypto Scrape df.xlsx', sheet_name='Sheet1')
# drop the Unnamed column
df.drop(columns=['Unnamed: 0'], inplace=True)
# apply literal_eval to all columns to convert them from strings to dicts
df = df.applymap(literal_eval)
# create a dict of dataframes in a dict comprehension
df_dict = {col: pd.json_normalize(df[col]) for col in df.columns}
# add a ticker column
for k, d in df_dict.items():
df_dict[k]['ticker'] = k
# combine all the dicts into a single dataframe
df = pd.concat(df_dict.values()).reset_index(drop=True)
# convert the time column to a datetime format
df.time = pd.to_datetime(df.time, unit='s')
# set the time column as the index
df.set_index('time', inplace=True)
# to get only close values under each ticker with time as the index
dfp = df[['close', 'ticker']].pivot(columns='ticker', values='close')
# set the column and index name as None, if desired
dfp.columns.name = None
dfp.index.name = None
顯示前 5 行和前 5 列dfp
# display(dfp.iloc[:5, :5])
ADA ALGO ATOM BAT BCH
2017-12-27 0.00 0.0 0.0 0.3200 2710.64
2017-12-28 0.00 0.0 0.0 0.6891 2484.96
2017-12-29 0.00 0.0 0.0 0.4013 2619.32
2017-12-30 0.59 0.0 0.0 0.4001 2209.96
2017-12-31 0.71 0.0 0.0 0.5910 2371.83
添加回答
舉報