简体   繁体   中英

Pandas DataFrame convert date string

beginner question. If you run the below:

df = pd.read_html('https://coinmarketcap.com/currencies/veritaseum/historical-data/?start=20180101&end=20180105')

df

it creates a table:

[           Date   Open*    High     Low  Close**   Volume  Market Cap
 0  Jan 05, 2018  366.43  395.69  356.26   380.11   426543   746285248
 1  Jan 04, 2018  376.58  397.02  353.17   368.46   734384   766956544
 2  Jan 03, 2018  378.55  395.28  352.05   376.05  1000590   770974464
 3  Jan 02, 2018  343.79  393.92  335.60   377.54  2011340   700168512
 4  Jan 01, 2018  338.14  357.18  325.33   343.12   890956   688681984]

but when you try creating a DataFrame:

data_table = pd.DataFrame(df)

data_table

Nothing comes out. Is it due to the Date being a string? How can you overcome it? Thanks in advance.

First need select first value of list of DataFrames returned by read_html and then use to_datetime , if want DatetimeIndex add set_index :

url = 'https://coinmarketcap.com/currencies/veritaseum/historical-data/?start=20180101&end=20180105'
df = pd.read_html(url)[0]
df['Date'] = pd.to_datetime(df['Date'], format='%b %d, %Y')
df = df.set_index('Date')
print(df)
             Open*    High     Low  Close**   Volume  Market Cap
Date                                                            
2018-01-05  366.43  395.69  356.26   380.11   426543   746285248
2018-01-04  376.58  397.02  353.17   368.46   734384   766956544
2018-01-03  378.55  395.28  352.05   376.05  1000590   770974464
2018-01-02  343.79  393.92  335.60   377.54  2011340   700168512
2018-01-01  338.14  357.18  325.33   343.12   890956   688681984

Or use parameters parse_dates and if need DatetimeIndex also add index_col :

df = pd.read_html(url, index_col=0, parse_dates=[0])[0]
print(df)
             Open*    High     Low  Close**   Volume  Market Cap
Date                                                            
2018-01-05  366.43  395.69  356.26   380.11   426543   746285248
2018-01-04  376.58  397.02  353.17   368.46   734384   766956544
2018-01-03  378.55  395.28  352.05   376.05  1000590   770974464
2018-01-02  343.79  393.92  335.60   377.54  2011340   700168512
2018-01-01  338.14  357.18  325.33   343.12   890956   688681984

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM