简体   繁体   English

熊猫:将列中的列表转换为行

[英]Pandas: Transpose a list in column into rows

So I have pandas dataframe df_dates as below. 所以我有pandas dataframe df_dates如下。

   PERSON_ID   MIN_DATE   MAX_DATE
0  000099-48 2016-02-01 2017-03-20
1     000184 2016-02-05 2017-01-19
2  000461-48 2016-03-07 2017-03-20
3  000791-48 2016-02-01 2017-03-07
4  000986-48 2016-02-01 2017-03-17
5     001617 2016-02-01 2017-02-20
6  001768-48 2016-02-01 2017-03-20
7     001937 2016-02-01 2017-03-17
8  002223-48 2016-02-04 2017-03-16
9  002481-48 2016-02-05 2017-03-17

I am trying to add all dates between the Min and Max as row each Person_ID. 我试图将Min和Max之间的所有日期添加为每个Person_ID的行。 Here is what tried. 这是尝试过的。

df_dates.groupby('PERSON_ID').apply(lambda x: pd.date_range(x['MIN_DATE'].values[0], x['MAX_DATE'].values[0]))

But what I get with this is, is there any way to transpose that series into rows for each Person_ID? 但我得到的是,有没有办法将该系列转换为每个Person_ID的行? or any other better way of doing it? 或任何其他更好的方式吗?

PERSON_ID
0-L2ID        DatetimeIndex(['2016-08-05', '2016-08-06', '20...
0-LlID        DatetimeIndex(['2016-02-03', '2016-02-04', '20...
000099-48     DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000184        DatetimeIndex(['2016-02-05', '2016-02-06', '20...
000276        DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000461-48     DatetimeIndex(['2016-03-07', '2016-03-08', '20...
000493-48     DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000615-48     DatetimeIndex(['2016-02-02', '2016-02-03', '20...
000791-48     DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000986-48     DatetimeIndex(['2016-02-01', '2016-02-02', '20...
dtype: object

Here is what I am trying achieve: 这是我正在努力实现的目标:

PERSON_ID   Date
000099-48   2/1/2016
000099-48   2/2/2016
000099-48   2/3/2016
000099-48   2/4/2016
:
:
000099-48   3/18/2016
000099-48   3/19/2016
000099-48   3/20/2016
000184  2/5/2016
000184  2/6/2016
000184  2/7/2016
:
:
000184  1/17/2017
000184  1/18/2017
000184  1/19/2017

You can reshape using melt , then perform a groupby and resample : 您可以使用melt重塑,然后执行groupbyresample

# Reshape via melt to get in the proper format for a resample.
df = df.melt(id_vars=['PERSON_ID'], value_vars=['MIN_DATE', 'MAX_DATE'], value_name='DATE')

# Set the index and drop unnecessary columns.
df = df.set_index('DATE').drop('variable', axis=1)

# Perform a groupby and resample.
df = df.groupby('PERSON_ID', group_keys=False).resample('D').ffill().reset_index()

The resulting output: 结果输出:

           DATE  PERSON_ID
0    2016-02-01  000099-48
1    2016-02-02  000099-48
2    2016-02-03  000099-48
3    2016-02-04  000099-48
...         ...        ...
3976 2017-03-14  002481-48
3977 2017-03-15  002481-48
3978 2017-03-16  002481-48
3979 2017-03-17  002481-48

Option 1 选项1

d = pd.concat({
        p: pd.Series(pd.date_range(s, e)) for i, p, s, e in df.itertuples()
    })

d.rename_axis(
    ['PERSON_ID', None]
).reset_index('PERSON_ID', name='Date').reset_index(drop=True)

      PERSON_ID       Date
0     000099-48 2016-02-01
1     000099-48 2016-02-02
...
414      000184 2016-02-05
415      000184 2016-02-06
...
764   000461-48 2016-03-07
765   000461-48 2016-03-08
...
1143  000791-48 2016-02-01
1144  000791-48 2016-02-02
...
1544  000986-48 2016-02-01
1545  000986-48 2016-02-02
...
1955     001617 2016-02-01
1956     001617 2016-02-02
...
2341  001768-48 2016-02-01
2342  001768-48 2016-02-02
...
2755     001937 2016-02-01
2756     001937 2016-02-02
...

Option 2 选项2

lol = [pd.date_range(t.MIN_DATE, t.MAX_DATE).tolist() for t in df.itertuples()]
lns = [len(l) for l in lol]
pd.DataFrame(dict(
        PERSON_ID=df.PERSON_ID.values.repeat(lns), Date=np.concatenate(lol)
    ))[['PERSON_ID', 'Date']]

      PERSON_ID       Date
0     000099-48 2016-02-01
1     000099-48 2016-02-02
...
414      000184 2016-02-05
415      000184 2016-02-06
...
764   000461-48 2016-03-07
765   000461-48 2016-03-08
...
1143  000791-48 2016-02-01
1144  000791-48 2016-02-02
...
1544  000986-48 2016-02-01
1545  000986-48 2016-02-02
...
1955     001617 2016-02-01
1956     001617 2016-02-02
...
2341  001768-48 2016-02-01
2342  001768-48 2016-02-02
...
2755     001937 2016-02-01
2756     001937 2016-02-02
...

You can also continue what you were already doing before, but convert the datetimeindex into a string, and then use str.split to create new rows 您也可以继续以前的操作,但将datetimeindex转换为字符串,然后使用str.split创建新行

For example: 例如:

df = df.groupby('PERSON_ID').apply(lambda x: pd.date_range(x['MIN_DATE'].values[0], x['MAX_DATE'].values[0])).reset_index()
df_dates = df.rename(columns={0: 'Dates'})

Create Function to convert to string. 创建函数以转换为字符串。

def get_date_string(x):
     return ", ".join([d.strftime('%Y-%m-%d') for d in x])

df_dates['Dates'] = df_dates['Dates'].apply(get_date_string)

Split the string into new rows. 将字符串拆分为新行。

s = df_dates['Dates'].str.split(", ").apply(pd.Series, 1).stack()
s.index = s.index.droplevel(-1)
s.name = 'Dates'

Join with PERSON_ID column. 加入PERSON_ID列。

del df[0]
print(df.join(s))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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