从Pandas Dataframe中的一行获取某些列值并将它们添加到另一个数据帧中(Taking certain column values from one row in a Pandas Data

编程入门 行业动态 更新时间:2024-10-11 05:28:02
从Pandas Dataframe中的一行获取某些列值并将它们添加到另一个数据帧中(Taking certain column values from one row in a Pandas Dataframe and adding them into another dataframe)

我想将数据帧df特定行的某些列值复制到另一个名为bestdf数据帧中

在这里,我创建一个空数据帧(称为bestdf ):

new_columns = ['DATE', 'PRICE1', 'PRICE2'] bestdf = pd.DataFrame(columns = new_columns) bestdf.set_index(['DATE'])

。我已经找到df的某一行并将该行分配给变量last_time :

last_time = df.iloc[-1] print last_time

给我

DATETIME PRC 2016-10-03 00:07:39.295000 335.82

然后我想从DATETIME列中获取2016-10-03并将其放入我的其他数据帧( bestdf )的DATE列中。 我还想把PRC放到空数据帧的PRICE1列中。 我想bestdf看起来像这样:

DATE PRICE1 PRICE2 2016-10-03 335.82

这是我到目前为止所得到的?

sample_date = str(last_time).split() best_price = sample_date[2] sample_date = sample_date[0] bestdf['DATE'] = sample_date bestdf['PRICE1'] = best_price

这似乎不起作用。 仅供参考我还想把它放到循环中(其中last_time将被修改,每次将新值写入新行)。 我目前正在努力使功能正确。

请帮忙!

谢谢

I would like to copy certain column values from a specific row in my dataframe df to another dataframe called bestdf

Here I create an empty dataframe (called bestdf):

new_columns = ['DATE', 'PRICE1', 'PRICE2'] bestdf = pd.DataFrame(columns = new_columns) bestdf.set_index(['DATE'])

.I've located a certain row out of df and assigned the row to a variable last_time:

last_time = df.iloc[-1] print last_time

gives me

DATETIME PRC 2016-10-03 00:07:39.295000 335.82

I then want to take the 2016-10-03 from the DATETIME column and put that into the DATE column of my other dataframe (bestdf). I also want to take the PRC and put it into the PRICE1 column of my empty dataframe. I want bestdf to look like this:

DATE PRICE1 PRICE2 2016-10-03 335.82

Here is what I've got so far?

sample_date = str(last_time).split() best_price = sample_date[2] sample_date = sample_date[0] bestdf['DATE'] = sample_date bestdf['PRICE1'] = best_price

This doesn't seem to work though. FYI I also want to put this into a loop (where last_time will be amended and each time the new values will be written to a new row). I'm just currently trying to get the functionality correct.

Please help!

Thanks

最满意答案

有多种方法可以做你想要做的事情:你也可以把你的问题分解成多个部分。 这样您就可以应用不同的步骤来解决它们。

这是一个例子:

import pandas as pd from datetime import datetime data = [{'DATETIME': '2016-10-03 00:07:39.295000', 'PRC': 335.29}, {'DATETIME': '2016-10-03 00:07:39.295000', 'PRC': 33.9}, {'DATETIME': '2016-10-03 00:07:39.295000', 'PRC': 10.9}] df = pd.DataFrame.from_dict(data, orient='columns') df

输出:

DATETIME PRC 0 2016-10-03 00:07:39.295000 335.29 1 2016-10-03 00:07:39.295000 33.90 2 2016-10-03 00:07:39.295000 10.90

代码继续:

bestdf = df[df['PRC'] > 15].copy() # we filter data from original df and make a copy bestdf.columns = ['DATE','PRICE1'] # we change columns as we need bestdf['PRICE2'] = None bestdf

输出:

DATE PRICE1 PRICE2 0 2016-10-03 00:07:39.295000 335.29 None 1 2016-10-03 00:07:39.295000 33.90 None

代码继续:

bestdf['DATE'] = bestdf['DATE'].apply(lambda value: value.split(' ')[0]) # we change column format based on how we need it to be bestdf

输出:

DATE PRICE1 PRICE2 0 2016-10-03 335.29 None 1 2016-10-03 33.90 None

我们也可以使用datetime对象做同样的事情。 不一定必须是字符串。

There are multiple ways to do what are you are looking to do: Also you can break your problem down into multiple pieces. That way you will be able to apply different steps to solve them.

Here is an example:

import pandas as pd from datetime import datetime data = [{'DATETIME': '2016-10-03 00:07:39.295000', 'PRC': 335.29}, {'DATETIME': '2016-10-03 00:07:39.295000', 'PRC': 33.9}, {'DATETIME': '2016-10-03 00:07:39.295000', 'PRC': 10.9}] df = pd.DataFrame.from_dict(data, orient='columns') df

output:

DATETIME PRC 0 2016-10-03 00:07:39.295000 335.29 1 2016-10-03 00:07:39.295000 33.90 2 2016-10-03 00:07:39.295000 10.90

code continue:

bestdf = df[df['PRC'] > 15].copy() # we filter data from original df and make a copy bestdf.columns = ['DATE','PRICE1'] # we change columns as we need bestdf['PRICE2'] = None bestdf

output:

DATE PRICE1 PRICE2 0 2016-10-03 00:07:39.295000 335.29 None 1 2016-10-03 00:07:39.295000 33.90 None

code continue:

bestdf['DATE'] = bestdf['DATE'].apply(lambda value: value.split(' ')[0]) # we change column format based on how we need it to be bestdf

output:

DATE PRICE1 PRICE2 0 2016-10-03 335.29 None 1 2016-10-03 33.90 None

We can also do the same thing with datetime objects also. Doesn't have to be string necessarily.

更多推荐

本文发布于:2023-07-09 10:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   数据   Pandas   Dataframe   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!