I want to get slopes of dataset in the dataframe (either using linear regression model or sk-learn model).
df1:
   A     B     C     D  
0  15    25    55    100
1  15.5  25.5  56    101
2  14.8  24.5  54.2  99.8
3  15.5  25.5  55.5  102
4  16    26    57    108
I want to get slopes of each dolumn ('A', 'B', 'C', 'D') in the form of pd.Series. Can you help me on this? Thank you.
The output I want is something like below (I used just dummy numbers, not real slopes!):
slopes:
A  2.5
B  2.8
C  3.1
D  3.3
                Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns like a spreadsheet or SQL table, or a dict of Series objects. .
pandas get rows. We can use .loc [] to get rows. Note the square brackets here instead of the parenthesis (). The syntax is like this: df.loc [row, column]. column is optional, and if left blank, we can get the entire row. Because Python uses a zero-based index, df.loc [0] returns the first row of the dataframe.
How to Slice a DataFrame in Pandas #1 Checking the Version of Pandas. #2 Importing a Data Set in to Python. One of the most common operations that people use with Pandas is to read some kind... #3 Creating a DataFrame. Besides creating a DataFrame by reading a file, you can also create one via a ...
The inner square brackets define a Python list with column names, whereas the outer brackets are used to select the data from a pandas DataFrame as seen in the previous example. The returned data type is a pandas DataFrame:
I believe this does it, it's a simple linear regression with numpy
import numpy as np
slopes = df.apply(lambda x: np.polyfit(df.index, x, 1)[0])
>>> slopes
A    0.20
B    0.20
C    0.35
D    1.70
And if you want to visualize the data and the fitted slopes:
for i in df.columns:
    plt.scatter(df.index, df[i], label=i)
    plt.plot(np.polyval(np.polyfit(df.index, df[i], 1), df.index))
plt.legend()
plt.show()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With