Extract date from pandas.core.series.Series in pandas dataframe columns

丶灬走出姿态 提交于 2021-01-29 06:47:01

问题


For downloading German bank holidays via a web api and converting the json data into a pandas dataframe I use the following code (python 3):

import datetime
import requests
import pandas as pd


now  = datetime.datetime.now()
year = now.year
URL  ='https://feiertage-api.de/api/?jahr='+ str(year)
r    = requests.get(URL)
df   = pd.DataFrame(r.json())

The goal is a pandas dataframe looking like (picture = section of the dataframe):

The Problem: "columns" are pandas.core.series.Series and I cannot figure out how to extract the date using various versions of

df['BW'].str.split(", ", n = 0, expand = True) 

See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html

Please, can anyone help me to turn df into a "proper" dataframe with columns that only contain dates?


回答1:


One approach would be to do df.applymap(lambda x: '' if pd.isna(x) else x['datum']):

In [21]: df.applymap(lambda x: '' if pd.isna(x) else x['datum'])
Out[21]:
                                   BW          BY          BE          BB          HB  ...          SN          ST          SH          TH    NATIONAL
1. Weihnachtstag           2019-12-25  2019-12-25  2019-12-25  2019-12-25  2019-12-25  ...  2019-12-25  2019-12-25  2019-12-25  2019-12-25  2019-12-25
2. Weihnachtstag           2019-12-26  2019-12-26  2019-12-26  2019-12-26  2019-12-26  ...  2019-12-26  2019-12-26  2019-12-26  2019-12-26  2019-12-26
Allerheiligen              2019-11-01  2019-11-01                                      ...
Augsburger Friedensfest                2019-08-08                                      ...
Buß- und Bettag                        2019-11-20                                      ...  2019-11-20
Christi Himmelfahrt        2019-05-30  2019-05-30  2019-05-30  2019-05-30  2019-05-30  ...  2019-05-30  2019-05-30  2019-05-30  2019-05-30  2019-05-30
Frauentag                                          2019-03-08                          ...
Fronleichnam               2019-06-20  2019-06-20                                      ...  2019-06-20                          2019-06-20
Gründonnerstag             2019-04-18                                                  ...
Heilige Drei Könige        2019-01-06  2019-01-06                                      ...              2019-01-06
Karfreitag                 2019-04-19  2019-04-19  2019-04-19  2019-04-19  2019-04-19  ...  2019-04-19  2019-04-19  2019-04-19  2019-04-19  2019-04-19
Mariä Himmelfahrt                      2019-08-15                                      ...
Neujahrstag                2019-01-01  2019-01-01  2019-01-01  2019-01-01  2019-01-01  ...  2019-01-01  2019-01-01  2019-01-01  2019-01-01  2019-01-01
Ostermontag                2019-04-22  2019-04-22  2019-04-22  2019-04-22  2019-04-22  ...  2019-04-22  2019-04-22  2019-04-22  2019-04-22  2019-04-22
Ostersonntag                                                   2019-04-21              ...
Pfingstmontag              2019-06-10  2019-06-10  2019-06-10  2019-06-10  2019-06-10  ...  2019-06-10  2019-06-10  2019-06-10  2019-06-10  2019-06-10
Pfingstsonntag                                                 2019-06-09              ...
Reformationstag            2019-10-31                          2019-10-31  2019-10-31  ...  2019-10-31  2019-10-31  2019-10-31  2019-10-31
Tag der Arbeit             2019-05-01  2019-05-01  2019-05-01  2019-05-01  2019-05-01  ...  2019-05-01  2019-05-01  2019-05-01  2019-05-01  2019-05-01
Tag der Deutschen Einheit  2019-10-03  2019-10-03  2019-10-03  2019-10-03  2019-10-03  ...  2019-10-03  2019-10-03  2019-10-03  2019-10-03  2019-10-03



回答2:


You could try to fix the shape of the input (i.e. the json response) before constructing the data frame & then reshape as needed.

example:

import datetime
import requests
import pandas as pd


now  = datetime.datetime.now()
year = now.year
URL  ='https://feiertage-api.de/api/?jahr='+ str(year)
r    = requests.get(URL)
df = pd.DataFrame(
  [(k1,k2,k3,v3) 
   for k1, v1 in r.json().items() 
   for k2, v2 in v1.items() 
   for k3, v3 in v2.items()]
)

df.head()
# Outputs: 
    0                    1        2           3
0  BW          Neujahrstag    datum  2019-01-01
1  BW          Neujahrstag  hinweis
2  BW  Heilige Drei Könige    datum  2019-01-06
3  BW  Heilige Drei Könige  hinweis
4  BW       Gründonnerstag    datum  2019-04-18

# it is easier to see what is happening if we
# fix the column names

df.columns = ['State', 'Holiday', 'value_type', 'value']
pivoted = df[df.value_type == 'datum'].set_index(['Holiday', 'State']).value.unstack(-1)

pivoted.head()
# Outputs:

State                            BB          BE          BW     ...              SN          ST          TH
Holiday                                                         ...
1. Weihnachtstag         2019-12-25  2019-12-25  2019-12-25     ...      2019-12-25  2019-12-25  2019-12-25
2. Weihnachtstag         2019-12-26  2019-12-26  2019-12-26     ...      2019-12-26  2019-12-26  2019-12-26
Allerheiligen                   NaN         NaN  2019-11-01     ...             NaN         NaN         NaN
Augsburger Friedensfest         NaN         NaN         NaN     ...             NaN         NaN         NaN
Buß- und Bettag                 NaN         NaN         NaN     ...      2019-11-20         NaN         NaN

[5 rows x 17 columns]


来源:https://stackoverflow.com/questions/55357264/extract-date-from-pandas-core-series-series-in-pandas-dataframe-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!