How to select random column from dataframe in pandas?What does the “yield” keyword do?How to randomly select an item from a list?How do I sort a dictionary by value?Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
Why are non-collision-resistant hash functions considered insecure for signing self-generated information
Why do banks “park” their money at the European Central Bank?
Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?
Antonym of "billable"
Did a flight controller ever answer Flight with a no-go?
If all stars rotate, why was there a theory developed that requires non-rotating stars?
What is this symbol: semicircles facing each other?
Would the Republic of Ireland and Northern Ireland be interested in reuniting?
Would it be possible to have a GMO that produces chocolate?
What would be the challenges to taking off and landing a typical passenger jet at FL300?
Position a tabular on the corner of a slide
How to find out the average duration of the peer-review process for a given journal?
Is a player able to change alignment midway through an adventure?
Is "The life is beautiful" incorrect or just very non-idiomatic?
How do we calculate energy of food?
Is there any example of one country devastating a third?
Was it ever possible to target a zone?
How much authority do teachers get from *In Loco Parentis*?
How do I get toddlers to stop asking for food every hour?
Did anyone try to find the little box that held Professor Moriarty and his wife after the Enterprise D crashed?
Why would an IIS hosted site prompt for AD account credential if accessed through a hostname or IP, but not through servername?
Understanding Parallelize methods
Can a Rogue PC teach an NPC to perform Sneak Attack?
Disambiguation of "nobis vobis" and "nobis nobis"
How to select random column from dataframe in pandas?
What does the “yield” keyword do?How to randomly select an item from a list?How do I sort a dictionary by value?Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to select random value from data frame.
For example:
if df=[a,b,c,d]
I want to select random from these 4 options
python pandas
add a comment |
I want to select random value from data frame.
For example:
if df=[a,b,c,d]
I want to select random from these 4 options
python pandas
2
df.sample
pandas.pydata.org/pandas-docs/stable/reference/api/…
– WeNYoBen
Mar 27 at 17:32
add a comment |
I want to select random value from data frame.
For example:
if df=[a,b,c,d]
I want to select random from these 4 options
python pandas
I want to select random value from data frame.
For example:
if df=[a,b,c,d]
I want to select random from these 4 options
python pandas
python pandas
edited Mar 31 at 7:06
marc_s
602k136 gold badges1150 silver badges1288 bronze badges
602k136 gold badges1150 silver badges1288 bronze badges
asked Mar 27 at 17:31
john terryjohn terry
426 bronze badges
426 bronze badges
2
df.sample
pandas.pydata.org/pandas-docs/stable/reference/api/…
– WeNYoBen
Mar 27 at 17:32
add a comment |
2
df.sample
pandas.pydata.org/pandas-docs/stable/reference/api/…
– WeNYoBen
Mar 27 at 17:32
2
2
df.sample
pandas.pydata.org/pandas-docs/stable/reference/api/…– WeNYoBen
Mar 27 at 17:32
df.sample
pandas.pydata.org/pandas-docs/stable/reference/api/…– WeNYoBen
Mar 27 at 17:32
add a comment |
2 Answers
2
active
oldest
votes
Your comments suggest you want to return a random column
rather than random row
. You can still achieve this using df.sample()
. You just have to specify the axis
.
import pandas as pd
d = (
'A' : [1,2,3,4],
'B' : [1,2,3,4],
'C' : [1,2,3,4],
'D' : [1,2,3,4],
)
df = pd.DataFrame(data = d)
#Return random Column
df_Col = df.sample(axis=1)
#Return random Row
df_Row = df.sample()
add a comment |
If you want to select certain column you can to something similar to indexing in numpy .
df_new = df[2]
df_new = c
print c[row_number]
First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.
Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.
– john terry
Mar 27 at 18:01
May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.
– Astro
Mar 27 at 18:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383314%2fhow-to-select-random-column-from-dataframe-in-pandas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your comments suggest you want to return a random column
rather than random row
. You can still achieve this using df.sample()
. You just have to specify the axis
.
import pandas as pd
d = (
'A' : [1,2,3,4],
'B' : [1,2,3,4],
'C' : [1,2,3,4],
'D' : [1,2,3,4],
)
df = pd.DataFrame(data = d)
#Return random Column
df_Col = df.sample(axis=1)
#Return random Row
df_Row = df.sample()
add a comment |
Your comments suggest you want to return a random column
rather than random row
. You can still achieve this using df.sample()
. You just have to specify the axis
.
import pandas as pd
d = (
'A' : [1,2,3,4],
'B' : [1,2,3,4],
'C' : [1,2,3,4],
'D' : [1,2,3,4],
)
df = pd.DataFrame(data = d)
#Return random Column
df_Col = df.sample(axis=1)
#Return random Row
df_Row = df.sample()
add a comment |
Your comments suggest you want to return a random column
rather than random row
. You can still achieve this using df.sample()
. You just have to specify the axis
.
import pandas as pd
d = (
'A' : [1,2,3,4],
'B' : [1,2,3,4],
'C' : [1,2,3,4],
'D' : [1,2,3,4],
)
df = pd.DataFrame(data = d)
#Return random Column
df_Col = df.sample(axis=1)
#Return random Row
df_Row = df.sample()
Your comments suggest you want to return a random column
rather than random row
. You can still achieve this using df.sample()
. You just have to specify the axis
.
import pandas as pd
d = (
'A' : [1,2,3,4],
'B' : [1,2,3,4],
'C' : [1,2,3,4],
'D' : [1,2,3,4],
)
df = pd.DataFrame(data = d)
#Return random Column
df_Col = df.sample(axis=1)
#Return random Row
df_Row = df.sample()
answered Mar 27 at 22:25
jonboyjonboy
622 silver badges15 bronze badges
622 silver badges15 bronze badges
add a comment |
add a comment |
If you want to select certain column you can to something similar to indexing in numpy .
df_new = df[2]
df_new = c
print c[row_number]
First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.
Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.
– john terry
Mar 27 at 18:01
May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.
– Astro
Mar 27 at 18:43
add a comment |
If you want to select certain column you can to something similar to indexing in numpy .
df_new = df[2]
df_new = c
print c[row_number]
First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.
Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.
– john terry
Mar 27 at 18:01
May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.
– Astro
Mar 27 at 18:43
add a comment |
If you want to select certain column you can to something similar to indexing in numpy .
df_new = df[2]
df_new = c
print c[row_number]
First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.
If you want to select certain column you can to something similar to indexing in numpy .
df_new = df[2]
df_new = c
print c[row_number]
First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.
answered Mar 27 at 17:44
AstroAstro
136 bronze badges
136 bronze badges
Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.
– john terry
Mar 27 at 18:01
May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.
– Astro
Mar 27 at 18:43
add a comment |
Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.
– john terry
Mar 27 at 18:01
May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.
– Astro
Mar 27 at 18:43
Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.
– john terry
Mar 27 at 18:01
Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.
– john terry
Mar 27 at 18:01
May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.
– Astro
Mar 27 at 18:43
May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.
– Astro
Mar 27 at 18:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55383314%2fhow-to-select-random-column-from-dataframe-in-pandas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
df.sample
pandas.pydata.org/pandas-docs/stable/reference/api/…– WeNYoBen
Mar 27 at 17:32