DATA READING AND CONNECTING USING FOR SENTENCE

Asked 2 years ago, Updated 2 years ago, 21 views

Until now, I have read and concatenated the data with the following code, but can I use the for statement to make the first 10 lines smart?

import numpy as np
import pandas aspd
from pandas import Series, DataFrame
import matplotlib.pyplot asplt
%matplotlib inline
da1 = pd.read_csv("data-1.csv", header = None)#
da2=pd.read_csv("data-2.csv", header=None)#
da3=pd.read_csv("data-3.csv", header=None)#
da4=pd.read_csv("data-4.csv", header=None)#
da5=pd.read_csv("data-5.csv", header=None)#
da6=pd.read_csv("data-6.csv", header=None)#
da7=pd.read_csv("data-7.csv", header=None)#
da8=pd.read_csv("data-8.csv", header=None)#
da9=pd.read_csv("data-9.csv", header=None)#
da10=pd.read_csv("data-10.csv", header=None)#

da_all=pd.concat([da1, da2, da3, da4, da5, da6, da7, da8, da9, da10], Axis=1, sort=False)
plt.plot(ch, da_all [0:12], linestyle='None', marker='o')

np.savetxt("data-all.csv", da_all, delimiter=",")#

An error occurred when I tried using the following code.
# Preprocessing

# Data Integration (for pre-transposition preprocessing)

for n1 in range (1,11):
    f"da{n1}" = pd.read_csv(f"201013-945-del-{n1}.csv", header=None)#


da_all=pd.concat([da1, da2, da3, da4, da5, da6, da7, da8, da9, da10], Axis=1, sort=False)
plt.plot(ch, da_all [0:12], linestyle='None', marker='o')

np.savetxt("201013-945-del-all.csv", da_all, delimiter=",")#

python

2022-09-29 22:08

2 Answers

I think you've already given your own answers...

 da_all=pd.concat(
  [pd.read_csv(f"data-{i+1}.csv", header=None) for i in range(10)],
  axis=1, sort=False
)


2022-09-29 22:08

I think the code below is a common code using for loop.

df_list=[]
for i in range (1,11):
    df_list.append(pd.read_csv(f"data-{i}.csv", header=None))

df_all=pd.concat(df_list,axis=1,sort=False)

I don't recommend it, but I think the code that the questioner wanted to write is as follows.

for n1 in range (1,11):
    exec("da"+str(n1)+"=pd.read_csv(f'data-{n1}.csv', header=None)")

da_all=pd.concat([da1, da2, da3, da4, da5, da6, da7, da8, da9, da10], Axis=1, sort=False)


2022-09-29 22:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.