Reputation: 1403
I have two arrays which consists of tuples (or you can think of 4 arrays).
Date Array1 Date Array2
27/10 47 27/10 34
28/10 34 28/10 27
4/11 42 5/11 23
6/11 26
Now I want to store this two arrays into a pandas data frame. Since both arrays have different length, this does not work.
The final pandas.DataFrame
has to look like this
Date Array1 Array2
27/10 47 34
28/10 34 27
4/11 42
5/11 23
6/11 26
With empy string or NaN inplace when there is no information for this specific date. Is there any neat way to do that?
Upvotes: 0
Views: 94
Reputation: 862691
If input are DataFrames
use concat
with set_index
for align both DataFrames
:
df = pd.concat([df1.set_index('Date'), df2.set_index('Date')], axis=1)
print (df)
Array1 Array2
27/10 47.0 34.0
28/10 34.0 27.0
4/11 42.0 NaN
5/11 NaN 23.0
6/11 26.0 NaN
EDIT:
If there is no year
information, pandas add some default - e.g. 1900
. It should be important if need ordering by dates like in calendar need to_datetime
first:
df1['Date'] = pd.to_datetime(df1['Date'], format='%d/%m')
df2['Date'] = pd.to_datetime(df2['Date'], format='%d/%m')
df = pd.concat([df1.set_index('Date'), df2.set_index('Date')], axis=1).reset_index()
print (df)
Date Array1 Array2
0 1900-10-27 47.0 34.0
1 1900-10-28 34.0 27.0
2 1900-11-04 42.0 NaN
3 1900-11-05 NaN 23.0
4 1900-11-06 26.0 NaN
And if need your original format use dt.strftime
:
df['Date'] = df['Date'].dt.strftime('%d/%m')
print (df)
Date Array1 Array2
0 27/10 47.0 34.0
1 28/10 34.0 27.0
2 04/11 42.0 NaN
3 05/11 NaN 23.0
4 06/11 26.0 NaN
Upvotes: 1