[英]How do I create a new Pandas Dataframe Column with data from another column
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? 如何使用pandas数据框操作执行此操作?
EDIT: 编辑:
Using nanojohn's solution I got this output. 使用nanojohn的解决方案,我得到了这个输出。 Pretty close. 八九不离十。 Still need to break up that last entry in 2017-02-01. 仍然需要在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
): 您可以尝试如下使用.apply()
方法(假设您的DataFrame在名为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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.