【问题标题】:How to develop a function如何开发功能
【发布时间】:2019-10-31 03:52:17
【问题描述】:

我是 python 的新手,我正在学习它。我已经编写了以下代码,但我无法对其进行任何功能。有人可以帮帮我吗?

import pandas as pd
import numpy as np
f = open('1.csv', 'r')
df = pd.read_csv(f, usecols=[0], sep="\t", index_col=False)
Primary_List = df.values.tolist()
x = 0
y = len(Primary_List)
for i in range(x, y):
x = i
MyMatrix = Primary_List[x:x + 10]
print(MyMatrix)

【问题讨论】:

  • 你的意思是你想在你的代码中定义一个函数?

标签: pandas list csv numpy matrix


【解决方案1】:

您可以创建一个传入文件名的函数,然后您可以使用此代码读取和打印许多 csv 文件。

def createMatrix(filename):
    f = open(filename, 'r')
    df = pd.read_csv(f, usecols=[0], sep="\t", index_col=False)
    Primary_List = df.values.tolist()
    x = 0
    y = len(Primary_List)
    for i in range(x, y):
    x = i
    MyMatrix = Primary_List[x:x + 10]
    return MyMatrix

print(createMatrix('1.csv'))
print(createMatrix('2.csv'))
print(createMatrix('3.csv'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2010-12-09
    • 2018-08-31
    相关资源
    最近更新 更多