简体   繁体   English

如何使用来自另一列的数据创建新的Pandas Dataframe列

[英]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.

相关问题 如何根据 pandas dataframe 中另一列的多个值在一列中创建值列表? - How do I create a list of values in a column from several values from another column in a pandas dataframe? 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 如何从另一列的所有值创建新的列名并按 pandas dataframe 中的另一列创建新列名? - how to create new column names from another column all values and agg by another column in pandas dataframe? 如何将数据从 Pandas 数据帧的一列拆分为新数据帧的多列 - How do I split data out from one column of a pandas dataframe into multiple columns of a new dataframe 如何通过从另一列中的句子中提取单词来在 pandas 数据框中创建一个新列? - How can I create a new column in a pandas data frame by extracting words from sentences in another column? 如何使用另一个 dataframe 中的值在 dataframe 中创建新列? - How do I create a new column in a dataframe using values from another dataframe? 如何向现有 dataframe 添加新列并用另一列的部分数据填充它? - How do I add a new column to an existing dataframe and fill it with partial data from another column? 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 如何创建一个新列,该列是在Pandas Dataframe中相似值分组在一起的另一列的最大值? - How do I create a new column which is the max of another column where similar values grouped together in Pandas Dataframe? Pandas DataFrame:如何从另一列的数值中创建数值? - Pandas DataFrame: How do I create numerical values out of numerical values from another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM