Please tell me how to connect automatically generated data frames repeatedly?

Asked 2 years ago, Updated 2 years ago, 43 views

For example, automatically generate data frames x repeatedly and randomly as shown below, and
Please tell me how to automatically connect it to one data frame df.

import pandas as pd
import numpy as np

i = 0
arr = [ ]
for i in range (3):
    x = pd.DataFrame(np.random.rand(3))
    arr[i] = x
    df=merge([arr[0],arr[i]])

print(df)
IndexError Traceback (most recent call last)
<ipython-input-4-3499f5327871>in<module>()
      6 for i in range (3):
      7 x = pd.DataFrame(np.random.rand(3))
---->8arr[i]=x
      9 df = merge ([arr[0], arr[i]])
     10 

IndexError:list assignment index out of range

python pandas

2022-09-30 21:44

1 Answers

Update:This might be a little simpler

import pandas as pd
import numpy as np

arr = [ ]
for i in range (3):
  x = pd.DataFrame(np.random.rand(3))
  arr.append(x)

df=pd.concat(arr,ignore_index=True)

print(arr)
print(df)

You can do it like this.

import pandas as pd
import numpy as np

df = pd.DataFrame()
for i in range (3):
  x = pd.DataFrame(np.random.rand(3))
  df = pd.concat ([df, x], ignore_index = True)

print(df)

By the way, if you want to keep individual DataFrame as arr[] for later use, the following is true:

import pandas as pd
import numpy as np

arr = [ ]
df = pd.DataFrame()
for i in range (3):
  x = pd.DataFrame(np.random.rand(3))
  arr.append(x)
  df = pd.concat ([df, x], ignore_index = True)

print(arr)
print(df)


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.