pandas own:
shuffle_data = df. Sample (frac = 1). Reset_index (drop = True)
sklearn:
import numpy as np
import pandas as pd
from sklearn.utils import shuffle as reset
def train_test_split(data, test_size=0.3, shuffle=True, random_state=None):
'''Split DataFrame into random train and test subsets
Parameters
----------
data : pandas dataframe, need to split dataset.
test_size : float
If float, should be between 0.0 and 1.0 and represent the
proportion of the dataset to include in the train split.
random_state : int, RandomState instance or None, optional (default=None)
If int, random_state is the seed used by the random number generator;
If RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by `np.random`.
shuffle : boolean, optional (default=None)
Whether or not to shuffle the data before splitting. If shuffle=False
then stratify must be None.
'''
if shuffle:
data = reset(data, random_state=random_state)
train = data[int(len(data)*test_size):].reset_index(drop = True)
test = data[:int(len(data)*test_size)].reset_index(drop = True)
return train, test
Read More:
- Solve the error of panda index unalignable Boolean series provided as indexer
- Extracting Data from XML (Using Python to Access Web Data)
- Panda error in modifying line name index does not support mutable operations
- Python: How to Reshape the data in Pandas DataFrame
- [leetcode] 295. Find Median from Data Stream Python
- Python: How to parses HTML, extracts data, and generates word documents
- Python 3.X error: valueerror: data type must provide an itemsize
- [CHM] Python: How to Extract CHM Data
- Python opens the table and appears pandas.errors.ParserError: Error tokenizing data. C error:
- Decode PUP data Error when reading radar data [How to Solve]
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed
- Linux installs Python and upgrades Python
- Python Error: pip install mysql-connector-python failed
- Pandas read_csv pandas.errors.ParserError: Error tokenizing data
- [How to Fix] TypeError: Cannot cast array data from dtype(‘float64‘) to dtype(‘<U32‘)….
- [Solved] Python Error: tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
- Opencv-python Install is Stuck Error: running setup.py bdist_wheel for opencv-python