使用Pandas将列从一个数据帧添加到另一个具有不同列名的数据帧

2024-09-28 23:33:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为prices的数据帧,它包含两列:时间戳和收盘价。内容如下:

Timestamp        Close
1/1/2017 0:00   966.6
1/1/2017 1:00   963.87
1/1/2017 2:00   963.97
1/1/2017 3:00   962.83

我有另一个名为output的数据帧,其内容如下:

^{pr2}$

我要做的是将价格数据框中的收盘价附加到上面的输出数据框中以获得如下所示的数据帧:

created_at        count       close
1/1/2017 0:00     5           966.6
1/1/2017 1:00     1           963.87
1/1/2017 2:00     1           963.97
1/1/2017 3:00     1           962.83

我知道我可以合并两个数据帧,然后使用

output.drop['Timestamp'], axis=1)

我可以使用\

output.dropna()

但是我不能合并不同列上的两个文件。我该怎么做?更新代码如下:

import pandas as pd

path1 = r'C:\Users\Ahmed Ismail Khalid\Desktop\Bullcrap Testing Delete Later\Bitcoin Prices Hourly Based.csv'
path2 = r'C:\Users\Ahmed Ismail Khalid\Desktop\Bullcrap Testing Delete Later\adam3us.csv'
path3 = r'C:\Users\Ahmed Ismail Khalid\Desktop\Bullcrap Testing Delete Later\ascending and final.csv'

df1 = pd.read_csv(path1)
df2 = pd.read_csv(path2)
df3 = pd.read_csv(path3)

output = pd.merge(df1, df2, how="inner", on="created_at") #column_name should be common in both dataframe. how represents type of intersection. In your case it will be inner(INNER JOIN)

df4 = output.created_at.value_counts().rename_axis('created_at').reset_index(name='adam3us_tweets')

df4 = df4.sort_values(by=['created_at'])


# output the dataframe df4
print(df4,'\n\n')


df4.to_csv('results.csv', encoding='utf-8',index=False)

任何和所有的帮助将不胜感激。在

谢谢


Tags: csv数据outputdeletetestingusersatpd