I have a Pandas dataframe created from a dict of lists. I want to split up those entries under the dates and create a new column called 'Story'.
2017-01-31 2017-02-01
Gates, Bill. [[SPGC-14075, 0.5]] [0]
Jobs, Steve. [[SPGC-14075, 3.5]] [0]
Jobs, Steve. [[SPGC-9456, 2.5]] [0]
White, John ANDERSON. [[SPGC-14075, 1.75]] [[SPGC-9456, 5.25]]
Ideal Output:
Story 2017-01-31 2017-02-01
Gates, Bill. SPGC-14075 0.5 0
Jobs, Steve. SPGC-14075 3.5 0
Jobs, Steve. SPGC-94562 .5 0
White, John ANDERSON. SPGC-14075 1.75 0
White, John ANDERSON. SPGC-9456 0 5.25
How do I go about doing this using pandas dataframe operations?
EDIT:
Using nanojohn's solution I got this output. Pretty close. Still need to break up that last entry in 2017-02-01.
2017-01-31 2017-02-01 Story
Gates, Bill. 0.50 0 SPGC-14075
Jobs, Steve. 3.50 0 SPGC-14075
Jobs, Steve. 2.50 0 SPGC-9456
White, John ANDERSON. 1.75 [SPGC-9456, 5.25] SPGC-14075
You can try using the .apply()
method as follows (assuming that your DataFrame is in a variable called df
):
df['Story'] = df['2017-01-31'].apply(lambda x: x[0][0])
df['2017-01-31'] = df['2017-01-31'].apply(lambda x: x[0][1])
df['2017-02-01'] = df['2017-02-01'].apply(lambda x: x[0])
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.