本文介绍了python,pd.read_csv成熟性导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用

 pd.read_csv

重复输入为第一次导入

a repetitive importation asat first time I import

node_t1 = pd.read_csv('5mins/nodes/N122')
node_t2 = pd.read_csv('10mins/nodes/N122')
node_t3 = pd.read_csv('15mins/nodes/N122')

但是我需要导入#次作为N1,N2,...,N121,N122第二次:

but I need to import # times as N1, N2, ..., N121, N122as the second time:

node_t1 = pd.read_csv('5mins/nodes/N121')
node_t2 = pd.read_csv('10mins/nodes/N121')
node_t3 = pd.read_csv('15mins/nodes/N121')

以此类推.

其他问题,您可以在代码中看到一个重复的名称(如"N122")

Other question, as you can see into the code there is a repetitive name (as 'N122')

node_t1 = pd.read_csv('5mins/nodes/N122')
node_t2 = pd.read_csv('10mins/nodes/N122')
node_t3 = pd.read_csv('15mins/nodes/N122')

有一种简单的方法可以将代码"N122"替换为"N"为

there is a simple method to substitute in the code 'N122' to 'N' as

node_t1 = pd.read_csv('5mins/nodes/N')
node_t2 = pd.read_csv('10mins/nodes/N')
node_t3 = pd.read_csv('15mins/nodes/N')

因此,对于每个节点,我都像以前一样保留相同的代码,而对于其他代码,我只更改了一次节点名称(N122,N121等).

so I keep the same code, like before, for each node and with other code I change only one time the node name (N122, N121,...).

有一个简单的方法可以做到这一点吗?

There is a simple method to do this?

推荐答案

可以使用格式化字符串和类似这样的循环

That can be done using a formatted string and a loop like:

def read_nodes(node_number):
    n1 = pd.read_csv('5mins/nodes/N%d' % node_number)
    n2 = pd.read_csv('10mins/nodes/N%d' % node_number)
    n3 = pd.read_csv('15mins/nodes/N%d' % node_number)
    return n1, n2, n3


for i in range(150):
    node_t1, node_t2, node_t3 = read_nodes(i)

    # process nodes for node i here
    ....

这篇关于python,pd.read_csv成熟性导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 01:04