0

I am having difficulties trying to append columns of different rows together.

For example,

enter image description here

Also, to note that A and B are in the form of dictionary, where you will see both dates and values.

However, my desired output is:

enter image description here

I have tried coding it out, but to no avail. Please assist.
for ric in ric_list:
    df = pd.DataFrame(ric_dict[ric])
    df['Date'] = pd.to_datetime(df['DATE'])
    df.set_index('Date', inplace=True)
    df = df.drop(columns=['DATE'])
    df.columns = [ric]
    print(ric)
    print(df.tail())
    rawData = pd.concat([rawData,df], ignore_index=False)
fauxpas
  • 93
  • 1
  • 9

1 Answers1

1

I think if you look for merging you can easily find a solution, here is a simple example to show the actual pandas function. I would suggest using the dates as index, then outer merge using the indexes as keys. A simple example here, but if you look for merging you can find a lot of documentation, e.g., using the indexes is not needed, see merge documentation.

import pandas as pd
import numpy as np

a = pd.DataFrame(data=np.arange(6), columns=['a'])
b = pd.DataFrame(data=np.arange(4), index=[2,3,4,5], columns=['b'])
# print(a)
# print(b)
m = pd.merge(a, b, left_index=True, right_index=True, how='outer')
# or m = a.join(b)
print(m)

returns:

   a    b
0  0  NaN
1  1  NaN
2  2  0.0
3  3  1.0
4  4  2.0
5  5  3.0

you can then change/substitute the NaN if you like or need so.

Peruz
  • 403
  • 3
  • 10