Convert a list of lists into a Pandas Dataframe
$begingroup$
I am trying to convert a list of lists which looks like the following into a Pandas Dataframe
[['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.
pandas
$endgroup$
add a comment |
$begingroup$
I am trying to convert a list of lists which looks like the following into a Pandas Dataframe
[['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.
pandas
$endgroup$
$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46
add a comment |
$begingroup$
I am trying to convert a list of lists which looks like the following into a Pandas Dataframe
[['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.
pandas
$endgroup$
I am trying to convert a list of lists which looks like the following into a Pandas Dataframe
[['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
I am basically trying to convert each item in the array into a pandas data frame which has four columns. What would be the best approach to this as pd.Dataframe does not quite give me what I am looking for.
pandas
pandas
edited Jun 5 '18 at 18:51
Emre
8,54011935
8,54011935
asked Jan 5 '18 at 18:40
Aravind VeluchamyAravind Veluchamy
51113
51113
$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46
add a comment |
$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46
$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46
$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
from pandas import DataFrame
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
DataFrame.from_records(data)
$endgroup$
1
$begingroup$
You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
$endgroup$
– Juan Ignacio Gil
Jan 11 '18 at 10:14
$begingroup$
Is there a way to specify the imports more specifically? E.g. I want to specify thatDataFrame["Team"]
must refer to the first item of each sublist (i.e.data[i][0]
) andDataFrame["Position"]
to refer to the last item of each sublist (i.e.data[i][-1]
)?
$endgroup$
– Ivo
Jan 17 at 15:20
$begingroup$
@Ivo: Use thecolumns
parameter of DataFrame.from_records.
$endgroup$
– Emre
Jan 17 at 21:27
add a comment |
$begingroup$
You can just directly define it as a data frame as follows:
import pandas as pd
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
data = pd.DataFrame(data)
$endgroup$
add a comment |
$begingroup$
Once you have the data:
import pandas as pd
data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
You can create dataframe from the transposing the data:
data_transposed = zip(data)
df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])
Another way:
df = pd.DataFrame(data)
df = df.transpose()
df.columns = ["Team", "Player", "Salary", "Role"]
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdatascience.stackexchange.com%2fquestions%2f26333%2fconvert-a-list-of-lists-into-a-pandas-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
from pandas import DataFrame
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
DataFrame.from_records(data)
$endgroup$
1
$begingroup$
You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
$endgroup$
– Juan Ignacio Gil
Jan 11 '18 at 10:14
$begingroup$
Is there a way to specify the imports more specifically? E.g. I want to specify thatDataFrame["Team"]
must refer to the first item of each sublist (i.e.data[i][0]
) andDataFrame["Position"]
to refer to the last item of each sublist (i.e.data[i][-1]
)?
$endgroup$
– Ivo
Jan 17 at 15:20
$begingroup$
@Ivo: Use thecolumns
parameter of DataFrame.from_records.
$endgroup$
– Emre
Jan 17 at 21:27
add a comment |
$begingroup$
from pandas import DataFrame
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
DataFrame.from_records(data)
$endgroup$
1
$begingroup$
You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
$endgroup$
– Juan Ignacio Gil
Jan 11 '18 at 10:14
$begingroup$
Is there a way to specify the imports more specifically? E.g. I want to specify thatDataFrame["Team"]
must refer to the first item of each sublist (i.e.data[i][0]
) andDataFrame["Position"]
to refer to the last item of each sublist (i.e.data[i][-1]
)?
$endgroup$
– Ivo
Jan 17 at 15:20
$begingroup$
@Ivo: Use thecolumns
parameter of DataFrame.from_records.
$endgroup$
– Emre
Jan 17 at 21:27
add a comment |
$begingroup$
from pandas import DataFrame
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
DataFrame.from_records(data)
$endgroup$
from pandas import DataFrame
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
DataFrame.from_records(data)
edited Jan 6 '18 at 0:58
answered Jan 5 '18 at 18:46
EmreEmre
8,54011935
8,54011935
1
$begingroup$
You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
$endgroup$
– Juan Ignacio Gil
Jan 11 '18 at 10:14
$begingroup$
Is there a way to specify the imports more specifically? E.g. I want to specify thatDataFrame["Team"]
must refer to the first item of each sublist (i.e.data[i][0]
) andDataFrame["Position"]
to refer to the last item of each sublist (i.e.data[i][-1]
)?
$endgroup$
– Ivo
Jan 17 at 15:20
$begingroup$
@Ivo: Use thecolumns
parameter of DataFrame.from_records.
$endgroup$
– Emre
Jan 17 at 21:27
add a comment |
1
$begingroup$
You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
$endgroup$
– Juan Ignacio Gil
Jan 11 '18 at 10:14
$begingroup$
Is there a way to specify the imports more specifically? E.g. I want to specify thatDataFrame["Team"]
must refer to the first item of each sublist (i.e.data[i][0]
) andDataFrame["Position"]
to refer to the last item of each sublist (i.e.data[i][-1]
)?
$endgroup$
– Ivo
Jan 17 at 15:20
$begingroup$
@Ivo: Use thecolumns
parameter of DataFrame.from_records.
$endgroup$
– Emre
Jan 17 at 21:27
1
1
$begingroup$
You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
$endgroup$
– Juan Ignacio Gil
Jan 11 '18 at 10:14
$begingroup$
You could refine it a bit more with: DataFrame.from_records(data, columns=['Team', 'Player', 'whatever-stat-is-that', 'position'])
$endgroup$
– Juan Ignacio Gil
Jan 11 '18 at 10:14
$begingroup$
Is there a way to specify the imports more specifically? E.g. I want to specify that
DataFrame["Team"]
must refer to the first item of each sublist (i.e. data[i][0]
) and DataFrame["Position"]
to refer to the last item of each sublist (i.e. data[i][-1]
)?$endgroup$
– Ivo
Jan 17 at 15:20
$begingroup$
Is there a way to specify the imports more specifically? E.g. I want to specify that
DataFrame["Team"]
must refer to the first item of each sublist (i.e. data[i][0]
) and DataFrame["Position"]
to refer to the last item of each sublist (i.e. data[i][-1]
)?$endgroup$
– Ivo
Jan 17 at 15:20
$begingroup$
@Ivo: Use the
columns
parameter of DataFrame.from_records.$endgroup$
– Emre
Jan 17 at 21:27
$begingroup$
@Ivo: Use the
columns
parameter of DataFrame.from_records.$endgroup$
– Emre
Jan 17 at 21:27
add a comment |
$begingroup$
You can just directly define it as a data frame as follows:
import pandas as pd
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
data = pd.DataFrame(data)
$endgroup$
add a comment |
$begingroup$
You can just directly define it as a data frame as follows:
import pandas as pd
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
data = pd.DataFrame(data)
$endgroup$
add a comment |
$begingroup$
You can just directly define it as a data frame as follows:
import pandas as pd
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
data = pd.DataFrame(data)
$endgroup$
You can just directly define it as a data frame as follows:
import pandas as pd
data = [['New York Yankees', 'Acevedo Juan', 900000, 'Pitcher'],
['New York Yankees', 'Anderson Jason', 300000, 'Pitcher'],
['New York Yankees', 'Clemens Roger', 10100000, 'Pitcher'],
['New York Yankees', 'Contreras Jose', 5500000, 'Pitcher']]
data = pd.DataFrame(data)
answered Jan 6 '18 at 5:43
LUSAQXLUSAQX
348416
348416
add a comment |
add a comment |
$begingroup$
Once you have the data:
import pandas as pd
data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
You can create dataframe from the transposing the data:
data_transposed = zip(data)
df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])
Another way:
df = pd.DataFrame(data)
df = df.transpose()
df.columns = ["Team", "Player", "Salary", "Role"]
$endgroup$
add a comment |
$begingroup$
Once you have the data:
import pandas as pd
data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
You can create dataframe from the transposing the data:
data_transposed = zip(data)
df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])
Another way:
df = pd.DataFrame(data)
df = df.transpose()
df.columns = ["Team", "Player", "Salary", "Role"]
$endgroup$
add a comment |
$begingroup$
Once you have the data:
import pandas as pd
data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
You can create dataframe from the transposing the data:
data_transposed = zip(data)
df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])
Another way:
df = pd.DataFrame(data)
df = df.transpose()
df.columns = ["Team", "Player", "Salary", "Role"]
$endgroup$
Once you have the data:
import pandas as pd
data = [['New York Yankees ', '"Acevedo Juan" ', 900000, ' Pitchern'],
['New York Yankees ', '"Anderson Jason"', 300000, ' Pitchern'],
['New York Yankees ', '"Clemens Roger" ', 10100000, ' Pitchern'],
['New York Yankees ', '"Contreras Jose"', 5500000, ' Pitchern']]
You can create dataframe from the transposing the data:
data_transposed = zip(data)
df = pd.DataFrame(data_transposed, columns=["Team", "Player", "Salary", "Role"])
Another way:
df = pd.DataFrame(data)
df = df.transpose()
df.columns = ["Team", "Player", "Salary", "Role"]
edited 13 hours ago
Coolio2654
1255
1255
answered Oct 3 '18 at 10:27
Paloma ManzanoPaloma Manzano
311
311
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fdatascience.stackexchange.com%2fquestions%2f26333%2fconvert-a-list-of-lists-into-a-pandas-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
$begingroup$
see this question in stack overflow: stackoverflow.com/questions/.../…
$endgroup$
– keramat
Jan 5 '18 at 18:46