Convert list to column in Python Dataframe
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a dataframe df which looks like this:
CustomerId Age
1 25
2 18
3 45
4 57
5 34
I have a list called "Price" which looks like this:
Price = [123,345,1212,11,677]
I want to add that list to the dataframe. Here is my code:
df['Price'] = Price
It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.
How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
python list pandas dataframe metadata
add a comment |
I have a dataframe df which looks like this:
CustomerId Age
1 25
2 18
3 45
4 57
5 34
I have a list called "Price" which looks like this:
Price = [123,345,1212,11,677]
I want to add that list to the dataframe. Here is my code:
df['Price'] = Price
It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.
How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
python list pandas dataframe metadata
5
Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.
– Dragonthoughts
Jul 6 '18 at 11:55
What solution was the one that worked???
– Adrian
Mar 30 at 14:58
add a comment |
I have a dataframe df which looks like this:
CustomerId Age
1 25
2 18
3 45
4 57
5 34
I have a list called "Price" which looks like this:
Price = [123,345,1212,11,677]
I want to add that list to the dataframe. Here is my code:
df['Price'] = Price
It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.
How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
python list pandas dataframe metadata
I have a dataframe df which looks like this:
CustomerId Age
1 25
2 18
3 45
4 57
5 34
I have a list called "Price" which looks like this:
Price = [123,345,1212,11,677]
I want to add that list to the dataframe. Here is my code:
df['Price'] = Price
It seems to work but when I print the dataframe the field called "Price" contains all the metadata information such as Name, Type... as well as the value of the Price.
How can I create a column called "Price" containing only the values of the Price list so that the dataframe looks like:
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
python list pandas dataframe metadata
python list pandas dataframe metadata
edited Jul 6 '18 at 12:52
Azat Ibrakov
4,47371732
4,47371732
asked Jul 6 '18 at 11:54
Giampaolo LevoratoGiampaolo Levorato
3715
3715
5
Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.
– Dragonthoughts
Jul 6 '18 at 11:55
What solution was the one that worked???
– Adrian
Mar 30 at 14:58
add a comment |
5
Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.
– Dragonthoughts
Jul 6 '18 at 11:55
What solution was the one that worked???
– Adrian
Mar 30 at 14:58
5
5
Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.
– Dragonthoughts
Jul 6 '18 at 11:55
Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.
– Dragonthoughts
Jul 6 '18 at 11:55
What solution was the one that worked???
– Adrian
Mar 30 at 14:58
What solution was the one that worked???
– Adrian
Mar 30 at 14:58
add a comment |
4 Answers
4
active
oldest
votes
In my Opinion, the most elegant solution is to use assign:
df.assign(Price=Price)
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
note that assign actually returns a DataFrame.
Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)
add a comment |
You can add pandas series as column.
import pandas as pd
df['Price'] = pd.Series(Price)
add a comment |
import pandas as pd
df['Price'] = pd.Series(Price)
if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.
add a comment |
I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:
import pandas as pd
df = pd.read_clipboard()
Price = [123,345,1212,11,677]
df.loc[:,'Price'] = Price
df
Generating this:
CustomerId Age Price
0 1 25 123
1 2 18 345
2 3 45 1212
3 4 57 11
4 5 34 677
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%2f51209933%2fconvert-list-to-column-in-python-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
In my Opinion, the most elegant solution is to use assign:
df.assign(Price=Price)
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
note that assign actually returns a DataFrame.
Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)
add a comment |
In my Opinion, the most elegant solution is to use assign:
df.assign(Price=Price)
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
note that assign actually returns a DataFrame.
Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)
add a comment |
In my Opinion, the most elegant solution is to use assign:
df.assign(Price=Price)
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
note that assign actually returns a DataFrame.
Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)
In my Opinion, the most elegant solution is to use assign:
df.assign(Price=Price)
CustomerId Age Price
1 25 123
2 18 345
3 45 1212
4 57 11
5 34 677
note that assign actually returns a DataFrame.
Assign creates a new Column 'Price' (left Price) with the content of the list Price (right Price)
edited Jul 6 '18 at 12:51
answered Jul 6 '18 at 12:45
Quickbeam2k1Quickbeam2k1
2,3271128
2,3271128
add a comment |
add a comment |
You can add pandas series as column.
import pandas as pd
df['Price'] = pd.Series(Price)
add a comment |
You can add pandas series as column.
import pandas as pd
df['Price'] = pd.Series(Price)
add a comment |
You can add pandas series as column.
import pandas as pd
df['Price'] = pd.Series(Price)
You can add pandas series as column.
import pandas as pd
df['Price'] = pd.Series(Price)
answered Jul 6 '18 at 11:59
hamza tunahamza tuna
9191612
9191612
add a comment |
add a comment |
import pandas as pd
df['Price'] = pd.Series(Price)
if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.
add a comment |
import pandas as pd
df['Price'] = pd.Series(Price)
if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.
add a comment |
import pandas as pd
df['Price'] = pd.Series(Price)
if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.
import pandas as pd
df['Price'] = pd.Series(Price)
if you use this you will not get the error if you have less values in the series than to your dataframe otherwise you will get the error that will tell you have less vales in the list and that can not be appended.
answered Nov 14 '18 at 11:26
akshit0106akshit0106
142
142
add a comment |
add a comment |
I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:
import pandas as pd
df = pd.read_clipboard()
Price = [123,345,1212,11,677]
df.loc[:,'Price'] = Price
df
Generating this:
CustomerId Age Price
0 1 25 123
1 2 18 345
2 3 45 1212
3 4 57 11
4 5 34 677
add a comment |
I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:
import pandas as pd
df = pd.read_clipboard()
Price = [123,345,1212,11,677]
df.loc[:,'Price'] = Price
df
Generating this:
CustomerId Age Price
0 1 25 123
1 2 18 345
2 3 45 1212
3 4 57 11
4 5 34 677
add a comment |
I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:
import pandas as pd
df = pd.read_clipboard()
Price = [123,345,1212,11,677]
df.loc[:,'Price'] = Price
df
Generating this:
CustomerId Age Price
0 1 25 123
1 2 18 345
2 3 45 1212
3 4 57 11
4 5 34 677
I copy pasted your example into a dataframe using pandas.read_clipboard and then added the column like this:
import pandas as pd
df = pd.read_clipboard()
Price = [123,345,1212,11,677]
df.loc[:,'Price'] = Price
df
Generating this:
CustomerId Age Price
0 1 25 123
1 2 18 345
2 3 45 1212
3 4 57 11
4 5 34 677
answered Nov 14 '18 at 11:33
JorgeJorge
1,47611022
1,47611022
add a comment |
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%2f51209933%2fconvert-list-to-column-in-python-dataframe%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
5
Please include your code, otherwise you are expecting people to guess what you wrote so that they can guess where you made an error.
– Dragonthoughts
Jul 6 '18 at 11:55
What solution was the one that worked???
– Adrian
Mar 30 at 14:58