简体   繁体   English

创建从 csv pandas 读取数据的值列表

[英]Create lists of values reading data from csv pandas

I need your help.我需要你的帮助。 I have a .csv "orders.csv" made in this way:我有一个以这种方式制作的 .csv“orders.csv”:

name          | number_of_orders | eurosxp
fried chips        100               2  
fried chicken       25               5
salad one           20               5
salad two           10               5

( and so on) ( 等等)

I'm trying to create a script in python to read this data and to do calculations, so with pandas, I'm trying to create a tab/list of values that I can recall in a framework...so I wrote this that doesn't work.我正在尝试在 python 中创建一个脚本来读取这些数据并进行计算,所以对于 pandas,我正在尝试创建一个我可以在框架中调用的值的选项卡/列表......所以我写了这个不起作用。 Someone can help?有人可以帮忙吗?

import numpy as np

import os
import math

import pandas as pd



class MenuNfeedback:

    data = pd.read_csv("orders.csv", header=None)


    def __init__(self) -> None:
        



      def name_dishes(self):
          '''dishes name'''
          for j in range(0, (self.data.iloc[j][0])):
              nameDishes = list(str(j) )
        
          return nameDishes


    def numb_ord(self):
        '''How many times was orderes'''
        orderNumb_list=[]
        orderNumb=self.data.iloc[i][1]
        for i in range(0, len(self.data.iloc[i][1])):
            orderNumb=self.data.iloc[i][1]
            orderNumb_list.append(orderNumb)

        return carbon_list



    def eurosxp(self):
        '''List of prices'''
        prices_f=[]
        for i in range(0, len(self.data.iloc[i][1])):
           prices_f.append(self.data.iloc[i][2])
        
        return prices_f

I see many errors and I started from few time to use/call functions我看到很多错误,我从很少时间开始使用/调用函数

Thank you谢谢

Your orders.csv file您的orders.csv文件

name          | number_of_orders | eurosxp
fried chips        100               2  
fried chicken       25               5
salad one           20               5
salad two           10               5

is not comma separated, so pd.read_csv("orders.csv", header=None) will not give expected DataFrame, you could get well-formed DataFrame following way不是逗号分隔,所以pd.read_csv("orders.csv", header=None)不会给出预期的DataFrame,你可以通过以下方式获得格式良好的DataFrame

import pandas as pd
df = pd.read_csv('orders.csv', header=None, sep=' {2,}', names=["name","number_of_orders","eurosxp"], skiprows=1)
print(df.shape)
print(df)

gives output给出输出

(4, 3)
            name  number_of_orders  eurosxp
0    fried chips               100        2
1  fried chicken                25        5
2      salad one                20        5
3      salad two                10        5

Explanation: I ignore 1st line of file and provide names of columns directly, I set separator to regular expressions meaning 2 or more spaces.说明:我忽略文件的第一行并直接提供列名,我将分隔符设置为表示 2 个或更多空格的正则表达式。 EDIT: code ameloriated as suggested in comment编辑:按照评论中的建议改进了代码

You can also read the file as fixed width formatted if the widths are consistent:如果宽度一致,您还可以将文件读取为fixed width formatted

pd.read_fwf("orders.csv", colspecs=[(0,14),(16, 33),(35,42)])

Output:输出:

            name  number_of_orders  eurosxp
0    fried chips               100        2
1  fried chicken                25        5
2      salad one                20        5
3      salad two                10        5

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

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