【发布时间】:2021-03-26 11:01:05
【问题描述】:
我有一个 Python 程序,可以从 csv 读取数据,我有 2 个问题。
-
假设在文件中我有从 1990 年到 2020 年的数据。我可以使用什么命令仅获取 2000 年以上的年份?
-
假设文件中的日期具有这种格式 '2000-12-02' 我如何将其添加到列表中,因为我认为它们目前是字符串,我无法进行预测,因为我需要字符串。
我将放在这里的代码与问题没有关联,只是为了让您可以看到我使用的导入和东西。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
regr = linear_model.LinearRegression()
df = pd.read_csv("net_monthly_average_earnings.csv")
print(df.head())
X = df[['Year']]
y = df[['Earnings']]
regr.fit(X, y)
earnings_predict = regr.predict(X)
plt.plot(X, y, 'o')
plt.plot(X, earnings_predict)
X_future = np.array(range(2021, 2030))
X_future = X_future.reshape(-1, 1)
future_predict = regr.predict(X_future)
plt.plot(X_future, future_predict, 'o')
plt.xlabel('Year')
plt.ylabel('Earning')
plt.title('Average salary in Romania + future predictions')
plt.show()
【问题讨论】:
标签: python pandas matplotlib scikit-learn