Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从CSV读取数据_Python_Pandas_Matplotlib_Scikit Learn - Fatal编程技术网

Python从CSV读取数据

Python从CSV读取数据,python,pandas,matplotlib,scikit-learn,Python,Pandas,Matplotlib,Scikit Learn,我有一个Python程序从csv读取数据,我有两个问题 比如说在文件中我有1990年到2020年的数据。我可以使用什么命令仅获取2000+之后的年份 假设文件中的日期具有这种格式“2000-12-02”,我如何将其添加到列表中,因为我认为它们目前是字符串,而我无法进行预测,因为我需要字符串 我将在这里输入的代码与问题无关,只是为了让您可以看到我使用的导入和内容 import pandas as pd import matplotlib.pyplot as plt import numpy a

我有一个Python程序从csv读取数据,我有两个问题

  • 比如说在文件中我有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()
    
    你可以使用“如果”语句来获得你想要的年份。 以你为例-

    if X>2000:
        print(X)
    
    或者,您可以使用SQL选择希望在DB中获得的年份(在创建init函数之后)

    要将日期放入列表中,请执行以下操作:

    import pandas as pd
    
    start = '2015-08-01' #YYY-MM-DD
    end = '2020-07-06'
    
    pd.date_range(start, end)
    
    # to start from today
    
    pd.date_range(pd.Timestamp.today(), end)
        
    

    没问题,伙计!
    def year_select(self,CURRENT_TIMESTAMP):
        self.cur.execute("SELECT YEAR IF>2000")
    
    import pandas as pd
    
    start = '2015-08-01' #YYY-MM-DD
    end = '2020-07-06'
    
    pd.date_range(start, end)
    
    # to start from today
    
    pd.date_range(pd.Timestamp.today(), end)