I have two columns from a dataframe that I want to get the Correlation Coefficient for: df['a'] and df['b'] there are around 15 or 20 rows of data.
I assign these to "col1" and "col2" and try and call the corr method:
col1 = df['a'] col2 = df['b'] corr = col1.corr(col2,method="pearson")
I get an error: 'float' object has no attribute 'shape'
If I import the stats library and try:
corr, pval=stats.pearsonr(col1,col2)
I get a correlation coefficient. So what did I do wrong on the first one?
In answer to one of the comments, I checked the type of col1 and col2 and they are both series. I thought this would work since I went to this link in the documentation: https://pandas.pydata.org/docs/reference/api/pandas.Series.corr.html Which gives no indication that you need to specify this is a series rather than a dataframe.
I also checked the type of the full dataframe:
print(type(df))
And it comes back as type dataframe The full dataframe is 21 columns with an index. I only want to get the Correlation Coefficient for two of the columns. Here is a subset of the data I get if I print col1 and col2:
col1:
Country
Indonesia 9.3659e-05
Japan 0.000388417
Canada 0.001638514
...
Name: a, dtype: objectcol2:
Country
Indonesia 65
Japan 194
Canada 167
...
Name: b, dtype: objectIs the index of Country causing the problem?
score:0
Either, df
is a Series:
>>> df
a 10.0
b 12.0
dtype: float64
or a columns of your dataframe has a wrong type:
>>> df
a b
0 10.0 20.0
1 12.0 22.0
>>> df.dtypes
a float64
b object
dtype: object
Credit To: stackoverflow.com
Related Query
- Pandas - 'Series' object has no attribute 'colNames' when using apply()
- AttributeError: 'ExceptionInfo' object has no attribute 'traceback' when using pytest to assert exceptions
- Getting error AttributeError: 'bool' object has no attribute 'transpose' when attempting to fit machine learning model
- pandas AttributeError: 'DataFrame' object has no attribute 'dt' when using apply on groupby
- Pandas error "AttributeError: 'DataFrame' object has no attribute 'add_categories'" when trying to add catorical values?
- NaTType' object has no attribute 'dt' error when comparing null and not null
- Error "'NoneType' object has no attribute 'offset'" when analysing GPX data
- Why I am receiving the error Error: module โstringโ has no attribute โlowerโ when I am using Pandas?
- Error Reading an Uploaded CSV Using Dask in Django: 'InMemoryUploadedFile' object has no attribute 'startswith'
- The error "AttributeError: 'list' object has no attribute 'values'" appears when I try to convert JSON to Pandas Dataframe
- How to solve 'numpy.ndarray' object has no attribute 'get_figure' error when subplotting?
- "AttributeError: 'float' object has no attribute 'all'" when using "where" method from Pandas
- Getting AttributeError: 'list' object has no attribute 'split' when using list comprehension
- Trying to send emails from python using smtplib and email.mime.multipart, getting the error "'Series' object has no attribute 'encode'"
- AttributeError: 'ElementTree' object has no attribute 'getiterator' when trying to import excel file
- Error: float object has no attribute notnull
- How to solve the Attribute error 'float' object has no attribute 'split' in python?
- Error in reading stock data : 'DatetimeProperties' object has no attribute 'weekday_name' and 'NoneType' object has no attribute 'to_csv'
- 'GroupedData' object has no attribute 'show' when doing doing pivot in spark dataframe
- Python Pandas Group By Error 'Index' object has no attribute 'labels'
- AttributeError: 'PandasExprVisitor' object has no attribute 'visit_Ellipsis', using pandas eval
- feather data storage library for python 'module' object has no attribute 'write_dataframe' error
- What does float' object has no attribute 'replace' when I try locale.atof in Pandas?
- Can anyone explain this error [AttributeError: 'DataFrame' object has no attribute 'to_numeric']
- Create column based on date conditions, but I get this error AttributeError: 'SeriesGroupBy' object has no attribute 'sub'?
- Getting "AttributeError: 'float' object has no attribute 'replace'" error while replacing string
- AttributeError: 'str' object has no attribute 'strftime' when modifying pandas dataframe
- Pandas-profiling error AttributeError: 'DataFrame' object has no attribute 'profile_report'
- Unable to drop column, object has no attribute error
- AttributeError: 'Series' object has no attribute 'startswith' when use pandas dataframe condition
More Query from same tag
- How to find the most frequent value based on dates in another column in Python
- Average of column B values per column A value
- Pandas conditional formatting not displaying background colors. (no errors)
- Combine data to create new data randomly on conditions
- Pandas merge two dataframes by taking the mean between the columns
- DataFrame - If statement resulting in NotImplementedError: couldn't find matching opcode for 'and_bdd' followed by a ValueError
- How does one use "takeable=True" and indexers in dataframe.set_value , especially with multiindex
- how to convert a Series of arrays into a single matrix in pandas/numpy?
- python: Dictionary key as row index, values as column headers. How do I refer back and select specific values in a df using a dictionary?
- Pandas: conditionally concatenate original columns with a string
- Extracting hourly data from 15 minutes interval data in python pandas
- How to clear values from columns with the values "No" instead of NaN?
- Find a value of a dictionary in dataframe column and modify it
- Deleting data in pandas
- Fill next / Previous row based on condition using groupby