简体   繁体   English

如何向现有 dataframe 添加新列并用另一列的部分数据填充它?

[英]How do I add a new column to an existing dataframe and fill it with partial data from another column?

I have a dataframe jobs screenshot of dataframe我有一个 dataframe作业dataframe 的屏幕截图

I need to add a new column 'year' to jobs data frame.我需要在工作数据框中添加一个新列“年份”。 This column should contain the corresponding year for each post_date (which is already a column).此列应包含每个 post_date 的相应年份(这已经是一列)。 For example: for post_date value 2017-08-16 'year' value should be 2017.例如:对于 post_date 值 2017-08-16 'year' 值应该是 2017。

I am unsure how to insert a new column while also pulling data from a pre-existing column.我不确定如何在插入新列的同时从预先存在的列中提取数据。

Use dt.year :使用dt.year

jobs['year'] = pd.to_datetime(jobs['post_date'], errors='coerce').dt.year

I would begin by transforming the column post_date into date format.我将首先将列 post_date 转换为日期格式。 After doing this, you could use a simple function to extract the year.完成此操作后,您可以使用简单的 function 来提取年份。

jobs["post_date"] =pd.to_datetime(jobs["post_date"])

should be enough to change it into a datetime type.应该足以将其更改为日期时间类型。 If it doesnt you should use datetime strpstring in order to tell python what is the specific format of the "post_date" column, so it to read it as a date.如果不是,您应该使用 datetime strpstring 来告诉 python “post_date”列的具体格式是什么,以便将其读取为日期。 After that do the following:之后执行以下操作:

jobs["year"] =jobs["post_date"].dt.year

If I understand your question correctly, you want to add a new column of values of years to the existing dataframe from a column in your current dataframe.如果我正确理解您的问题,您想从当前 dataframe 的列中向现有 dataframe 添加新的年份值列。 For extracting only the year values, you need to do some calculations first.要仅提取年份值,您需要先进行一些计算。 You can make use of pandas datetime.datetime and extract only the values of the year in your Post_date column.您可以使用 pandas datetime.datetime 并仅提取 Post_date 列中的年份值。 Have a look at this or this .看看这个这个 For storing these year values, you can simply do this:要存储这些年份值,您可以简单地执行以下操作:

jobs['year'] = jobs['post_date'].dt.year

暂无
暂无

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

相关问题 如何将数据从一个 dataframe 添加到另一个 dataframe 上的新列 - how to add data from one dataframe to a new column on another dataframe 如何使用来自另一列的数据创建新的Pandas Dataframe列 - How do I create a new Pandas Dataframe Column with data from another column 如何将计算结果添加到数据框中的新列? - How do I add the results from a calculation to a new column in a dataframe? 为什么我无法将现有数据添加到新的 dataframe 列中? - Why am I unable to add an existing data into a new dataframe column? 如何使用 Pandas 使用来自第二个数据帧但依赖于当前数据帧中不同现有列的值填充新列 - How To Fill New Column With Values From Second Dataframe but Dependent on Different Existing Column in Current Dataframe using Pandas append dataframe 将新数据添加到现有列 - append dataframe to add new data to existing column 如何使用条件从现有列在数据框中创建新列? - How do I create a new column in a dataframe from an existing column using conditions? 将新列添加到Pandas DataFrame,并用同一df的另一列填充第一个单词 - Add new column to Pandas DataFrame and fill with first word from another column from same df 基于部分字符串匹配从另一个 dataframe 填充一个数据框列 - Based on Partial string Match fill one data frame column from another dataframe 如何将新列添加到现有 pandas dataframe - How to add a new column to an existing pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM