Python 如何将数据帧列表拆分为两个列表?

Python 如何将数据帧列表拆分为两个列表?,python,pandas,Python,Pandas,这是我的初始数据帧df: col1 col2 col3 1 0.5 10 1 0.3 11 5 1.4 1 3 1.5 2 1 0.9 10 3 0.4 7 1 1.2 9 3 0.1 11 4 0.1 11 n = 3 # the value of "n" does

这是我的初始数据帧
df

col1    col2    col3
  1       0.5     10
  1       0.3     11
  5       1.4     1
  3       1.5     2
  1       0.9     10
  3       0.4     7
  1       1.2     9
  3       0.1     11
  4       0.1     11
n = 3 # the value of "n" does not matter
list_df = [df[i:i+n] for i in range(0, df.shape[0],n)]

list_df

[
  pd.DataFrame(
    col1    col2    col3
      1       0.5     10
      1       0.3     11
      5       1.4     1),
  pd.DataFrame(
    col1    col2    col3
      3       1.5     2
      1       0.9     10
      3       0.4     7),
  pd.DataFrame(
    col1    col2    col3
      1       1.2     9
      3       0.1     11
      4       0.1     11)
]
我将其转换为数据帧列表
list\u df

col1    col2    col3
  1       0.5     10
  1       0.3     11
  5       1.4     1
  3       1.5     2
  1       0.9     10
  3       0.4     7
  1       1.2     9
  3       0.1     11
  4       0.1     11
n = 3 # the value of "n" does not matter
list_df = [df[i:i+n] for i in range(0, df.shape[0],n)]

list_df

[
  pd.DataFrame(
    col1    col2    col3
      1       0.5     10
      1       0.3     11
      5       1.4     1),
  pd.DataFrame(
    col1    col2    col3
      3       1.5     2
      1       0.9     10
      3       0.4     7),
  pd.DataFrame(
    col1    col2    col3
      1       1.2     9
      3       0.1     11
      4       0.1     11)
]
如何将此列表随机分成两个数据帧列表:
list\u df1
list\u df2
,以便
list\u df1
包含70%的数据帧列表,而
list\u df2
包含其余的数据帧列表


我尝试使用掩蔽,但它不适用于数据帧列表。

您可以使用
numpy
中的
random\u integers
获取要保留的索引列表,然后过滤
list\u df

import numpy as np
import math

# compute what is 70% of the elements of list_df
n_70pct = math.floor(len(list_df)*0.7)

# take a sample of 70% of indexes in list_df
int_sample = np.random.random_integers(0,len(list_df), n_70pct ).tolist()

# keep in list_df1 the indices that are in int_sample
list_df1 = [ list_df[i] for i in int_sample]

# keep in list_df2 the indices that are not in int_sample
list_df2 = [ list_df[i] for i in range(0,len(list_df)) if i not in int_sample]

您想将列表分为n=2个分区吗?检查一下:@BelbaharRaouf:谢谢,但我觉得这和我需要的不一样。我有一个数据帧列表。实际上,
n
的值(即数据帧中的行数)并不重要?也适用于数据帧列表。@Cleb:是的,似乎非常接近我需要的。如何定义数据帧列表应拆分的索引?@Cleb:
list\u df1,list\u df1=np.split(list\u df,[6])
这似乎不起作用。