Home > Back-end >  prompting the user to load a csv in google colab
prompting the user to load a csv in google colab

Time:09-17

I am new to python yet somehow managed to compile a code to run ADF and Mann-Kendall tests on a csv file. What I actually want is to prompt the user to load a csv file. Consider the file has two columns; first with Dates and second with values. Once the csv is loaded, the ADF and M-K tests must be applied to the second column and display corresponding results.

import pandas as pd
Table = pd.read_csv('DRIVE LINK')
Table.shape

#Total null cells in each column
Table.isnull().sum()

#define function for ADF test
from statsmodels.tsa.stattools import adfuller
def adf_test(timeseries):
    #Perform Dickey-Fuller test:
    print ('Results of Dickey-Fuller Test:')
    dftest = adfuller(timeseries, autolag='AIC')
    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
    for key,value in dftest[4].items():
       dfoutput['Critical Value (%s)'%key] = value
    print (dfoutput)

#apply adf test on the series
adf_test(Table.iloc[:,1])

pip install pymannkendall

#perform Mann-Kendall Trend Test
import pymannkendall as mk
mk.original_test(Table.iloc[:,1])

I got this code while searching in google. But I'm not able to link it with my code.

from google.colab import files
uploaded = files.upload()

Nomatter what the file name is, once the user uploads a csv, both the tests should be performed. Is there a way to do that ? Thanks in advance.

CodePudding user response:

You have to extract the file name from the return value of the files.upload call. Here's some code that should do this for you. Be aware that you can upload multiple files with file.upload so it might not work as you expect in that case.

import io
import pandas as pd
from google.colab import files
print("Choose a single file")
uploaded = files.upload()
filename=[key for key in uploaded.keys()][0]
Table = pd.read_csv(io.BytesIO(uploaded[filename]))
  • Related