python 基于用户输入对不带Pandas的数据框进行切片

yh2wf1be  于 2023-01-08  发布在  Python
关注(0)|答案(2)|浏览(115)

我试图结束代码,但我有问题,如何切片 Dataframe 的基础上,用户的输入。有没有任何选项,这样做没有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("????")
mfuanj7w

mfuanj7w1#

将其转换为list,然后就可以像在普通的列表结构中那样进行切片。

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)
7cjasjjr

7cjasjjr2#

我找到了得到我所需要的东西的方法,也许这不是最好的方法,但很有效:)

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])

相关问题