Add numpy array values as columns to DataFrame by unique integer











up vote
-2
down vote

favorite












If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.










share|improve this question




















  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    2 days ago










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    2 days ago








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    2 days ago










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – Alexandre Nixon
    2 days ago












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    2 days ago

















up vote
-2
down vote

favorite












If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.










share|improve this question




















  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    2 days ago










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    2 days ago








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    2 days ago










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – Alexandre Nixon
    2 days ago












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    2 days ago















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.










share|improve this question















If I have a 3xn numpy array full of integers, how could I easily add those integers into a DataFrame like so:



input:



a = np.array(array([[2,10, 8],[2,9,4],[8,2,2],[8,9,10],[2,3,8]])


output:



Create empty DataFrame at first with columns being each unique integer from the numpy array.



In each row of this new DataFrame, I need to have each row in the numpy array appended to the unique columns. Each new integer should turn into a column separately containing the whole row.



Should look something like this before it gets to the [2,3,8] in the array:



Column:    2       4       8        9       10
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10]


Since the last entry in the numpy array has a 3 in it, and 3 is not yet in the column list, I'd like a new column to be made for it.



For example if the next item in the array after [2,3,8] is [1,89,2] then the DataFrame should now look like:



Column:    2       4       8        9       10         3        1     89
R |[2,9,4]|[8,2,2]|[2,9,4] |[8,2,2]| [2,9,4] | [1,89,2] | | |
O [8,2,2] [2,3,8] [2,3,8] [2,3,8]
W: [8,9,10] [1,89,2]
[1,89,2] [8,9,10]


1 and 89 are now created as rows awaiting the next item in the numpy array.



Then columns 1, 89 and 2 would contain the next item and so on. Hopefully this is more clear.



Technically I don't care how the data is stored, I assumed a dictionary at first but the DataFrame makes more sense when I look at it now. If there is a better way with a list, a dictionary or some other function I'm not aware of, please let me know if it makes sense now.



2nd Edit:



Sorry for the confusion guys.







python pandas numpy dictionary data-science






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









Jeffrey Ely

63




63








  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    2 days ago










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    2 days ago








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    2 days ago










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – Alexandre Nixon
    2 days ago












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    2 days ago
















  • 5




    Your desired output is not even valid Python...
    – Nils Werner
    2 days ago










  • It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
    – Jeffrey Ely
    2 days ago








  • 1




    @JeffreyEly, please edit so that the expected output makes sense.
    – Deepak Saini
    2 days ago










  • Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
    – Alexandre Nixon
    2 days ago












  • Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
    – Jeffrey Ely
    2 days ago










5




5




Your desired output is not even valid Python...
– Nils Werner
2 days ago




Your desired output is not even valid Python...
– Nils Werner
2 days ago












It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
– Jeffrey Ely
2 days ago






It was just an example. I have missing brackets and 5: isn't valid, apologies. Is there a way to do it as a valid dictionary though? Is the logic possible?
– Jeffrey Ely
2 days ago






1




1




@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
2 days ago




@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
2 days ago












Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
– Alexandre Nixon
2 days ago






Even in this example, you will find yourself having duplicate keys which are not supported in python dictionaries. Or is your idea to avoid keys which have already been added?
– Alexandre Nixon
2 days ago














Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
– Jeffrey Ely
2 days ago






Edited the original question with more examples. The point is to have one label, and append each new row that contains the next 3 integers into the existing label, and if it doesn't exist, add it as a new key. Is that possible? It works when I do it manually but I can't figure out a way to do it for a whole numpy array.
– Jeffrey Ely
2 days ago














1 Answer
1






active

oldest

votes

















up vote
0
down vote













dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer























  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    yesterday










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    19 hours ago











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373446%2fadd-numpy-array-values-as-columns-to-dataframe-by-unique-integer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer























  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    yesterday










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    19 hours ago















up vote
0
down vote













dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer























  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    yesterday










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    19 hours ago













up vote
0
down vote










up vote
0
down vote









dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you






share|improve this answer














dict1={'2':[[2,9,4],[8,2,2]],'4': [[8,2, 2],[8,9,10]],'8':[[2,9,4],[1,89,2]],'9':[[8,2,2],[2,3,8]],'10': [[2,9,4],[2,3,8]],'3': [[1,89,2],[2,3,8]]}

df3=pd.DataFrame(dict1)


I think this will help you







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday









WhatsThePoint

2,10342034




2,10342034










answered yesterday









Anuprita

285




285












  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    yesterday










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    19 hours ago


















  • Can you provide a bit of explanation as to why you think this will help?
    – WhatsThePoint
    yesterday










  • Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
    – Jeffrey Ely
    19 hours ago
















Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
yesterday




Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
yesterday












Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
– Jeffrey Ely
19 hours ago




Thank you but I need an actual function to add new data in that manner to either a DataFrame or a dictionary in that the '2' and the '4' etc values are coming from the actual array. Each value should have it's own label like that as I add in new data. Although I do appreciate the effort you put in. Thank you.
– Jeffrey Ely
19 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373446%2fadd-numpy-array-values-as-columns-to-dataframe-by-unique-integer%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory