简体   繁体   English

从HTTP标头的每一列中指定read_csv中每一列的数据类型

[英]Specify the data type of each column in read_csv from each column of an HTTP header

project_id = request.data['project']
list_fields = request.POST.getlist('headers')
type_fields = request.POST.getlist('type')

dataframe = pandas.read_csv(file_path, header=0)
                for field in list_fields:
                    for tipo in type_fields:
                        dataframe[field] = dataframe[field].astype(type)

how can I assign each type of data to a column according to the past in the request? 如何根据请求中的过去将每种类型的数据分配给列?

You can pass a list from the front with all the columns that you want to define, than pass another list with all the types that you want for the columns. 您可以从前面传递一个列表,其中包含要定义的所有列,而不是传递另一个列表,其中包含所需的所有列的类型。 After that you can cast those loops. 之后,您可以强制执行这些循环。

to_define_list_fields = request.POST.getlist('define')

type_list_fields = request.POST.getlist('types')                  

for dfield in to_define_list_fields:

   for type in type_list_fields:

       dataframe[dfield] = dataframe[dfield].astype(type)

When you use from_csv() , pandas will do an awful lot of type inference. 当您使用from_csv() ,pandas会进行很多类型推断。 In fact, more so than other methods such as convert_objects . 实际上,它比其他方法(例如convert_objects I asked a question about it here , which is somewhat related. here问了一个相关的问题。

I'm presuming that the front-end user has to specify the dtype of every column. 我假设前端用户必须指定每列的dtype。 In which case, it's a simple case of: 在这种情况下,这是一个简单的情况:

import numpy as np
import pandas as pd

df = pd.DataFrame({'a':[1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}, 
                   dtype=int)

list_fields = ['a', 'b', 'c']
list_types = [str, int, np.float64]

for field, dtype in zip(list_fields, list_types):
    df[field] = df[field].astype(dtype)

print(df.dtypes)

If the users don't have to specify the dtype of all of the fields then, on further thought, I think that would be a different topic entirely on how you filter/process user input. 如果用户不必指定所有字段的dtype,那么进一步考虑,我认为这完全是关于如何过滤/处理用户输入的另一主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM