Home > Software design >  Slice data frame without pandas based on user input
Slice data frame without pandas based on user input

Time:01-07

I'm trying to end the code, but I have problem how to slice data frame based on user's input. Is there any option to do this without pandas?

def dataSet_read():
enter = input('Enter file path:')
csvreader = csv.reader(open(enter))

head_inp = input('Has the file headers? Select Y or N:\n').upper()

header = []

if head_inp == 'Y':
    header = next(csvreader)
    print('\nFile headers:\n\n', header)
elif head_inp == 'N':
    print("'\nFile doesn't have headers")
else:
    print('Incorrect selection!!!')
    sys.exit()
         
with open(str(enter), "r") as csvfile:
    reader_variable = csv.reader(csvfile, delimiter = ",")

    rows_inp = input("\nPlease provide range which you'd like to see using ',', otherwise all dataframe will open all dataset.\n")
    if rows_inp == '':
        for row in reader_variable:
            print(row)
    else:
        print("????")

CodePudding user response:

cast it to list then you can slice like what it is in normal list structure.

enter = input('Enter file path:')
rows_inp = input("slice")
with open(enter , 'r') as f:
    reader_variable = csv.reader(f)
    reader_list= list(reader_variable)
    for row in reader_list[:rows_inp]:#if you want slice the whole data
        current_date = row[:rows_inp] #if you want slice per row
        print(current_date)
  

CodePudding user response:

I found the way to get what I need, maybe it's not the best approach but works :)

with open(str(enter), "r") as csvfile:
    reader_variable = csv.reader(csvfile, delimiter = ",")

    rows_inp = input("\nPlease provide range which you'd like to see using ',', otherwise all dataframe will open all dataset.\n")
    if rows_inp == '':
        for row in reader_variable:
            print(row)
    else:
        i, j = rows_inp.split(',')
        reader_list = list(reader_variable)
        print(reader_list[int(i):int(j) 1])
  • Related