简体   繁体   English

根据日期,使用来自另一个 dataframe 的值在 pandas dataframe 中创建一个新列

[英]Create a new column in pandas dataframe with values from another dataframe, based on date

I have 2 dataframes.我有 2 个数据框。

First dataframe contains PLN data:首先 dataframe 包含 PLN 数据:

         date  polarity  subjectivity
0  2022-03-25  0.118771      0.517508
1  2022-03-28  0.136532      0.555640
2  2022-04-04  0.447222      0.419444
3  2022-04-11 -0.022222      0.397222
4  2022-04-18 -0.098333      0.526111
5  2022-04-20 -0.077778      0.644444
6  2022-04-22  0.187500      0.437500
7  2022-04-25  0.350000      0.375000

The second dataframe contains stock data:第二个 dataframe 包含股票数据:

          open        high         low       close    adjclose    volume        date
0   277.559998  282.500000  272.059998  282.190002  282.190002  42549400  2022-03-28
1   286.950012  289.459991  279.799988  286.559998  286.559998  48898400  2022-03-29
2   283.040009  284.959991  275.029999  276.899994  276.899994  46348700  2022-03-30
3   277.820007  282.480011  272.700012  272.859985  272.859985  52344000  2022-03-31
4   273.750000  274.959991  262.670013  267.119995  267.119995  51653400  2022-04-01
5   267.279999  275.579987  266.130005  273.600006  273.600006  39712000  2022-04-04
6   272.540009  273.190002  258.200012  259.309998  259.309998  43661500  2022-04-05
7   249.339996  253.000000  240.029999  244.070007  244.070007  70383300  2022-04-06
8   244.410004  247.220001  234.779999  242.080002  242.080002  55799200  2022-04-07
9   239.169998  239.229996  230.619995  231.190002  231.190002  52409100  2022-04-08
10  222.139999  223.199997  216.600006  219.169998  219.169998  57520700  2022-04-11

What I want is to create both 'polarity' and 'subjetivity' columns in the second dataframe (the one with stock data) based on the date field the most efficient way .我想要的是以最有效的方式基于日期字段在第二个 dataframe(具有股票数据的那个)中创建“极性”和“主观性”列。

Edit Adding desired output:编辑添加所需的 output:

          open        high         low       close    adjclose    volume        date    polarity    subjetivity
0   277.559998  282.500000  272.059998  282.190002  282.190002  42549400  2022-03-28    0.136532       0.555640
1   286.950012  289.459991  279.799988  286.559998  286.559998  48898400  2022-03-29         NaN            NaN
2   283.040009  284.959991  275.029999  276.899994  276.899994  46348700  2022-03-30         NaN            NaN
3   277.820007  282.480011  272.700012  272.859985  272.859985  52344000  2022-03-31         NaN            NaN
4   273.750000  274.959991  262.670013  267.119995  267.119995  51653400  2022-04-01         NaN            NaN
5   267.279999  275.579987  266.130005  273.600006  273.600006  39712000  2022-04-04    0.447222       0.419444
6   272.540009  273.190002  258.200012  259.309998  259.309998  43661500  2022-04-05         NaN            NaN
7   249.339996  253.000000  240.029999  244.070007  244.070007  70383300  2022-04-06         NaN            NaN
8   244.410004  247.220001  234.779999  242.080002  242.080002  55799200  2022-04-07         NaN            NaN
9   239.169998  239.229996  230.619995  231.190002  231.190002  52409100  2022-04-08         NaN            NaN
10  222.139999  223.199997  216.600006  219.169998  219.169998  57520700  2022-04-11   -0.022222       0.397222
df2.merge(df1, how = 'left', on = 'date')

          open        high         low  ...        date  polarity  subjectivity
0   277.559998  282.500000  272.059998  ...  2022-03-28  0.136532      0.555640
1   286.950012  289.459991  279.799988  ...  2022-03-29       NaN           NaN
2   283.040009  284.959991  275.029999  ...  2022-03-30       NaN           NaN
3   277.820007  282.480011  272.700012  ...  2022-03-31       NaN           NaN
4   273.750000  274.959991  262.670013  ...  2022-04-01       NaN           NaN
5   267.279999  275.579987  266.130005  ...  2022-04-04  0.447222      0.419444
6   272.540009  273.190002  258.200012  ...  2022-04-05       NaN           NaN
7   249.339996  253.000000  240.029999  ...  2022-04-06       NaN           NaN
8   244.410004  247.220001  234.779999  ...  2022-04-07       NaN           NaN
9   239.169998  239.229996  230.619995  ...  2022-04-08       NaN           NaN
10  222.139999  223.199997  216.600006  ...  2022-04-11 -0.022222      0.397222

Try this尝试这个

df2.merge(df, on='date', how='left')

暂无
暂无

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

相关问题 根据另一列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in another column 熊猫:在一个数据框中创建新列,并根据与另一个数据框中的匹配键进行匹配 - Pandas: create new column in one dataframe with values based on matching key from another dataframe 如何根据 Pandas dataframe 中的日期值和条件创建新列 - How to create a new column based on Date Values & Condition in Pandas dataframe 如何基于另一列的值在pandas dataframe列中创建新值 - How to create new values in a pandas dataframe column based on values from another column 基于匹配来自另一个数据帧pandas的值的新列 - New column based on matching values from another dataframe pandas Pandas 数据框根据另一列的条件创建新行 - Pandas dataframe create new rows based on condition from another column Pandas 根据来自另一个 dataframe 的计数和条件创建新列 - Pandas Create new column based on a count and a condition from another dataframe 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 根据另一个 pandas 中的开始日期和结束日期列的条件创建新的 pandas dataframe - Create new pandas dataframe based on a condition on Start Date and End Date Column in another pandas 根据来自另一个熊猫数据框的列在熊猫数据框中创建新行 - Create new rows in a Pandas Dataframe based on a column from another pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM