我想将数据帧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_timegives me
DATETIME PRC 2016-10-03 00:07:39.295000 335.82I 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.82Here 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_priceThis 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') dfoutput:
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.90code 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 bestdfoutput:
DATE PRICE1 PRICE2 0 2016-10-03 00:07:39.295000 335.29 None 1 2016-10-03 00:07:39.295000 33.90 Nonecode continue:
bestdf['DATE'] = bestdf['DATE'].apply(lambda value: value.split(' ')[0]) # we change column format based on how we need it to be bestdfoutput:
DATE PRICE1 PRICE2 0 2016-10-03 335.29 None 1 2016-10-03 33.90 NoneWe can also do the same thing with datetime objects also. Doesn't have to be string necessarily.
更多推荐
发布评论