Prompting user to enter column names from a csv file (not using pandas framework)

倾然丶 夕夏残阳落幕 提交于 2020-06-09 05:39:46

问题


I am trying to get the column names from a csv file with nearly 4000 rows. There are about 14 columns. I am trying to get each column and store it into a list and then prompt the user to enter themselves at least 5 columns they want to look at. The user should then be able to type how many results they want to see (they should be the smallest results from that column). For example, if they choose clothing_brand, "8", the 8 least expensive brands are displayed.

So far, I have been able to use "with" and get a list that contains each column, but I am having trouble prompting the user to pick at least 5 of those columns.


回答1:


You can very well use the Python input to get the input from user, if you want to prompt no. of times, use the for loop to get inputs. Check Below code:

def get_user_val(no_of_entries = 5):
    print('Enter {} inputs'.format(str(no_of_entries)))
    val_list = []
    for i in range(no_of_entries):
            val_list.append(input('Enter Input {}:'.format(str(i+1))))
    return val_list

get_user_val()



回答2:


I hope I didn't misunderstand what you mean, the code below is what you want?

You can put the data into the dict then sorted it.

Solution1

from io import StringIO
from collections import defaultdict
import csv
import random
import pprint


def random_price():
    return random.randint(1, 10000)


def create_test_data(n_row=4000, n_col=14, sep=','):
    columns = [chr(65+i) for i in range(n_col)]  # A, B ...
    title = sep.join(columns)
    result_list = [title]
    for cur_row in range(n_row):
        result_list.append(sep.join([str(random_price()) for _ in range(n_col)]))
    return '\n'.join(result_list)


def main():
    if 'load CSV':
        test_content = create_test_data(n_row=10, n_col=5)

        dict_brand = defaultdict(list)
        with StringIO(test_content) as f:
            rows = csv.reader(f, delimiter=',')
            for idx, row in enumerate(rows):
                if idx == 0:  # title
                    columns = row
                    continue
                for i, value in enumerate(row):
                    dict_brand[columns[i]].append(int(value))

    pprint.pprint(dict_brand, indent=4, compact=True, width=120)
    user_choice = input('input columns (brand)')
    number_of_results = 5  # input('...')
    watch_columns = user_choice.split(' ')  # D E F

    for col_name in watch_columns:
        cur_brand_list = dict_brand[col_name]
        print(sorted(cur_brand_list, reverse=True)[:number_of_results])
        # print(f'{col_name} : {sorted(cur_brand_list)}')  # ASC
        # print(f'{col_name} : {sorted(cur_brand_list, reverse=True)}')  # DESC


if __name__ == '__main__':
    main()

defaultdict(<class 'list'>,
            {   'A': [9424, 6352, 5854, 5870, 912, 9664, 7280, 8306, 9508, 8230],
                'B': [1539, 1559, 4461, 8039, 8541, 4540, 9447, 512, 7480, 5289],
                'C': [7701, 6686, 1687, 3134, 5723, 6637, 6073, 1925, 4207, 9640],
                'D': [4313, 3812, 157, 6674, 8264, 2636, 765, 2514, 9833, 1810],
                'E': [139, 4462, 8005, 8560, 5710, 225, 5288, 6961, 6602, 4609]})
input columns (brand)C D
[9640, 7701, 6686, 6637, 6073]
[9833, 8264, 6674, 4313, 3812]

Solution2: Using Pandas

def pandas_solution(test_content: str, watch_columns= ['C', 'D'], number_of_results=5):
    with StringIO(test_content) as f:
        df = pd.read_csv(StringIO(f.read()), usecols=watch_columns,
                         na_filter=False)  # it can add performance (ignore na)
    dict_result = defaultdict(list)
    for col_name in watch_columns:
        dict_result[col_name].extend(df[col_name].sort_values(ascending=False).head(number_of_results).to_list())

    df = pd.DataFrame.from_dict(dict_result)
    print(df)

      C     D
0  9640  9833
1  7701  8264
2  6686  6674
3  6637  4313
4  6073  3812


来源:https://stackoverflow.com/questions/62247079/prompting-user-to-enter-column-names-from-a-csv-file-not-using-pandas-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!