I want to capture csv in panas together.

Asked 2 years ago, Updated 2 years ago, 41 views

Sorry for the rudimentary question.
Capture the files 1o_000.csv, 1o_001.csv...... and csv at once and
each
I'd like to make it data0, data1, data2,......

while(i<100):
    data+str(i)=pd.read_csv('1o_0'+str(i)+'.csv')
    i+=1

The data+str(i) part is wrong, but how should I write it to get the desired result?

python pandas

2022-09-30 21:24

2 Answers

The variable names data0, data1, and data2 cannot be generated, so
You will create a list of data[0], data[1], data[2]...
The arrangement is basic not only in python but also in all languages, so I think it would be better for you to understand it yourself without using the answers below as they are.
Official Tutorial

In addition to arranging receipts as codes, the number of digits in the number of digits in the file generation method is incorrect, so
You will also need to add a place-to-place process.

data=[]
while(i<100):
    data.append(pd.read_csv('1o_%03d.csv'%i))
    i+=1

Or if you know in advance that it is 100 pieces,

 data=[0]*100
while(i<100):
    data[i] = pd.read_csv('1o_%03d.csv'%i)
    i+=1

Both can be taken out in the following ways

print(data[5])#View 6th data


2022-09-30 21:24

I think you can get the desired result by using the list. What do you think?
<range(0,3) is for testing purposes.Rewrite to any number.
※ 質問1o_001.csv in the question statement assumes that 1o_001.csv is a typo.

import pandas as pd
data = [ ]
for i in range(0,3):
  fileName='1o_%03d.csv'%i
  data.append(pd.read_csv(fileName))
# answer-and-answer match
data2

You can declare dynamic variables using the built-in function exec as shown in the code below, but honestly, I think you should avoid coding like the following as much as possible.

import pandas as pd
for i in range(0,3):
  fileName='1o_%03d.csv'%i
  # Dynamically declare dataX and store results
  exec('data%d=pd.read_csv(fileName)'%i)
# answer-and-answer match
data2


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.