Reputation: 3038
I have a dataset which is basically a list of list
data = [[(datetime.datetime(2018, 12, 6, 10, 0), Decimal('7.0000000000000000')), (datetime.datetime(2018, 12, 6, 11, 0), Decimal('2.0000000000000000')), (datetime.datetime(2018, 12, 6, 12, 0), Decimal('43.6666666666666667')), (datetime.datetime(2018, 12, 6, 14, 0), Decimal('8.0000000000000000')), (datetime.datetime(2018, 12, 7, 9, 0), Decimal('12.0000000000000000')), (datetime.datetime(2018, 12, 7, 10, 0), Decimal('2.0000000000000000')), (datetime.datetime(2018, 12, 7, 11, 0), Decimal('2.0000000000000000')), (datetime.datetime(2018, 12, 7, 17, 0), Decimal('2.0000000000000000'))], [(datetime.datetime(2018, 12, 6, 10, 0), 28.5), (datetime.datetime(2018, 12, 6, 11, 0), 12.75), (datetime.datetime(2018, 12, 6, 12, 0), 12.15), (datetime.datetime(2018, 12, 6, 14, 0), 12.75), (datetime.datetime(2018, 12, 7, 9, 0), 12.75), (datetime.datetime(2018, 12, 7, 10, 0), 12.75), (datetime.datetime(2018, 12, 7, 11, 0), 12.75), (datetime.datetime(2018, 12, 7, 17, 0), 12.75)]]
It basically contains two lists each of them with a date
and metric
column. I need to extract the metric column values of each of the list and find a a coorelation between them.
Note: The dates are similar in each of the list
So first I load each of the list into pandas and set date index.
data1 = data[0]
data2 = data[1]
df1 = pd.DataFrame(data1)
df1[0] = pd.to_datetime(df1[0], errors='coerce')
df1.set_index(0, inplace=True)
df2 = pd.DataFrame(data2)
df2[0] = pd.to_datetime(df2[0], errors='coerce')
df2.set_index(0, inplace=True)
Now I merge the two data frames (both of them share the same dates).
df = pd.merge(df1,df2, how='inner', left_index=True, right_index=True)
Now my data frame looks something like this
1_x 1_y
0
2018-12-06 10:00:00 7.0000000000000000 28.50
2018-12-06 11:00:00 2.0000000000000000 12.75
2018-12-06 12:00:00 43.6666666666666667 12.15
2018-12-06 14:00:00 8.0000000000000000 12.75
2018-12-07 09:00:00 12.0000000000000000 12.75
2018-12-07 10:00:00 2.0000000000000000 12.75
2018-12-07 11:00:00 2.0000000000000000 12.75
2018-12-07 17:00:00 2.0000000000000000 12.75
Now I need to find the corelation between the two columns 1_x
and 1_y
. So I did this
df.iloc[:,0].corr(df.iloc[:,1])
But I get the following error
Traceback (most recent call last):
File "/home/souvik/Music/UI_Server2/test61.py", line 71, in <module>
print(df.iloc[:,0].corr(df.iloc[:,1]))
File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/pandas/core/series.py", line 1911, in corr
min_periods=min_periods)
File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/pandas/core/nanops.py", line 77, in _f
return f(*args, **kwargs)
File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/pandas/core/nanops.py", line 762, in nancorr
return f(a, b)
File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/pandas/core/nanops.py", line 770, in _pearson
return np.corrcoef(a, b)[0, 1]
File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/numpy/lib/function_base.py", line 2392, in corrcoef
c = cov(x, y, rowvar)
File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/numpy/lib/function_base.py", line 2302, in cov
avg, w_sum = average(X, axis=1, weights=w, returned=True)
File "/home/souvik/django_test/webdev/lib/python3.5/site-packages/numpy/lib/function_base.py", line 391, in average
if scl.shape != avg.shape:
AttributeError: 'float' object has no attribute 'shape'
I am not sure what's happening. The examples I saw online uses df['A].corr(df['B'])
to get the corelation between A
and B
. So what am I doing wrong?
Upvotes: 0
Views: 740
Reputation: 3967
Your column 1_x
has dtype=object
as can be observed from here:
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 8 entries, 2018-12-06 10:00:00 to 2018-12-07 17:00:00
Data columns (total 2 columns):
1_x 8 non-null object
1_y 8 non-null float64
dtypes: float64(1), object(1)
memory usage: 512.0+ bytes
So convert your column 1_x
to float
.
Use:
df['1_x'] = df['1_x'].astype(float)
df.iloc[:,0].corr(df.iloc[:,1])
# -0.11679873531647807
Upvotes: 2