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