Dividing Data Frames Using Panda's Beginner Repeating Statements

Asked 2 years ago, Updated 2 years ago, 83 views

Hello, Python. I'm a beginner at Panda's.

Declare the entire data as F1 I would like to declare it as data by dividing it 97 times by 2,000 each That is, F1_0 is from index number 0 to 1999, F1_1 is from 2000 to 3999, and the last F1_96 is from 2000 to 3999 To create a data frame, I wrote the following repetition statement.

n=0
for i in (0, 97):
  A=2000*(i+1)
  globals()['F1_{0}'.format(i)] = F1.loc[n:A]
  n = A

F1_0 calls well From F1_1, it says that it has not been defined, but it doesn't work.

I can't make a clean code for Python and Pandas, and I don't know what's wrong.

Masters, please help me.

loops pandas for python dataframe

2022-09-20 15:42

1 Answers

Do not create variables with globals().

F1_0, F1_1, ... When you want to do a variable like this, it's right to use an array. f1_list[0], f1_list[1], ... Like this. globals() It's clean and easy to write.

f1_list = [ F1.loc[i*2000:(i+1)*2000] for i in range(98) ]

print(f1_list[0])
print(f1_list[10])
print(len(f1_list))


2022-09-20 15:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.