简体   繁体   中英

Copying a column from one pandas dataframe to another

I have the following code where i try to copy the EXPIRATION from the recent dataframe to the EXPIRATION column in the destination dataframe:

recent = pd.read_excel(r'Y:\Attachments' + '\\' + '962021.xlsx')
print('HERE\n',recent)
print('HERE2\n', recent['EXPIRATION'])
destination= pd.read_excel(r'Y:\Attachments' + '\\' + 'Book1.xlsx')
print('HERE3\n', destination)
destination['EXPIRATION']= recent['EXPIRATION']
print('HERE4\n', destination)

The problem is that destination has less rows than recent so some of the lower rows in the EXPIRATION column from recent do not end up in the destination dataframe. I want all the EXPIRATION values from recent to be in the destination dataframe, even if all the other values are NaN.

Example Output:

HERE
    Unnamed: 0  IGNORE DATE_TRADE DIRECTION  EXPIRATION      NAME OPTION_TYPE  PRICE  QUANTITY     STRATEGY  STRIKE TIME_TRADE TYPE UNDERLYING
0           0      21   6/9/2021         B  08/06/2021  BNP FP E           C     12        12      CONDORI      12    9:23:40  ETF     NASDAQ
1           1      22   6/9/2021         B  16/06/2021  BNP FP E           P     12        12  GOLD/SILVER      12    10:9:19  ETF     NASDAQ
2           2      23   6/9/2021         B  16/06/2021      TEST           P     12        12      CONDORI      21   10:32:12  EQT       TEST
3           3      24   6/9/2021         B  22/06/2021      TEST           P     12        12  GOLD/SILVER      12    10:35:5  EQT     NASDAQ
4           4       0   6/9/2021         B  26/06/2021      TEST           P     12        12  GOLD/SILVER      12   10:37:11  ETF    FTSE100
HERE2
 0    08/06/2021
1    16/06/2021
2    16/06/2021
3    22/06/2021
4    26/06/2021
Name: EXPIRATION, dtype: object
HERE3
    Unnamed: 0  IGNORE  DATE_TRADE  DIRECTION  EXPIRATION  NAME  OPTION_TYPE  PRICE  QUANTITY  STRATEGY  STRIKE  TIME_TRADE  TYPE  UNDERLYING
0         NaN     NaN         NaN        NaN           2   NaN          NaN    NaN       NaN       NaN     NaN         NaN   NaN         NaN
1         NaN     NaN         NaN        NaN           1   NaN          NaN    NaN       NaN       NaN     NaN         NaN   NaN         NaN
2         NaN     NaN         NaN        NaN           3   NaN          NaN    NaN       NaN       NaN     NaN         NaN   NaN         NaN
HERE4
    Unnamed: 0  IGNORE  DATE_TRADE  DIRECTION  EXPIRATION  NAME  OPTION_TYPE  PRICE  QUANTITY  STRATEGY  STRIKE  TIME_TRADE  TYPE  UNDERLYING
0         NaN     NaN         NaN        NaN  08/06/2021   NaN          NaN    NaN       NaN       NaN     NaN         NaN   NaN         NaN
1         NaN     NaN         NaN        NaN  16/06/2021   NaN          NaN    NaN       NaN       NaN     NaN         NaN   NaN         NaN
2         NaN     NaN         NaN        NaN  16/06/2021   NaN          NaN    NaN       NaN       NaN     NaN         NaN   NaN         NaN

Joining is generally the best approach, but I see that you have no id column apart from native pandas indexing, and there are only Nans in destination, so if you are sure that ordering is not a problem you can just use:

>>> destination = pd.concat([recent,destination[['EXPIRATION']]], ignore_index=True, axis=1)

 Unnamed: 0  IGNORE  DATE_TRADE  DIRECTION  EXPIRATION     ...
0         NaN     NaN         NaN        NaN  08/06/2021   ...
1         NaN     NaN         NaN        NaN  16/06/2021   ...
2         NaN     NaN         NaN        NaN  16/06/2021   ...
3         NaN     NaN         NaN        NaN  22/06/2021   ...
4         NaN     NaN         NaN        NaN  26/06/2021   ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM