How do i split a dataframe (csv) in the ratio of 4:1 randomly and store them in two different variables ex- if there are ten rows from 1 to 10 in the dataframe, i want any 8 rows from it in variable 'a' and the remaining 2 rows in variable 'b'.
Asked
Active
Viewed 554 times
0
-
Typically, questions that are asked without code samples of what the asker has tried are closed as "off-topic" (and so don't get answered).. Next time, please include your code. – Rachel Gallen Feb 06 '19 at 22:19
1 Answers
0
I've never done this on a random basis but the basic approach would be:
- import pandas 2)
- read in your csv
- drop empty/null columns(avoid issues with these)
- create a new dataframe to put the split values into
- assign names to your new columns
- split values and combine the values (using apply/combine/lambda)
Code sample:
# importing pandas module
import pandas as pd
# read in csv file
data = pd.read_csv("https://mydata.csv")
# drop null values
data.dropna(inplace = True)
# create new data frame
new = data["ColumnName"].str.split(" ", n = 1, expand = True) #this 'split' code applies to splitting one column into two
# assign new name to first column
data["A"]= new[0] #8 concatenated values will go here
# making seperate last name column from new data frame
data["B"]= new[1] #last two [combined] values in go here
## other/different code required for concatenation of column values - look at this linked SO question##
# df display
data
Hope this helps

Rachel Gallen
- 27,943
- 21
- 72
- 81
-
In machine learning, from sklearn.model_selection import train_test_split xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size = 0.2, random_state = 0), this is the code when a supervised leraning algorithm is used. What would be the code when it is unsupervised. Basically there is no 'y' in that case. Hope you get my question – Rahul Ramaswamy Feb 07 '19 at 09:46
-
@RahulRamaswamy I understand your question. I thought you may be able to adapt the code by integrating a) code from the linked question and b) your existing knowledge and logic. Was just trying to help! – Rachel Gallen Feb 07 '19 at 09:50
-