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.
python pandas numpy dictionary data-science
add a comment |
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.
python pandas numpy dictionary data-science
5
Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18
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
Nov 19 at 11:20
1
@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22
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?
– nixon
Nov 19 at 11:24
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
Nov 19 at 11:33
add a comment |
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.
python pandas numpy dictionary data-science
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
python pandas numpy dictionary data-science
edited Nov 19 at 12:55
asked Nov 19 at 11:15
Jeffrey Ely
24
24
5
Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18
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
Nov 19 at 11:20
1
@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22
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?
– nixon
Nov 19 at 11:24
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
Nov 19 at 11:33
add a comment |
5
Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18
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
Nov 19 at 11:20
1
@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22
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?
– nixon
Nov 19 at 11:24
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
Nov 19 at 11:33
5
5
Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18
Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18
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
Nov 19 at 11:20
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
Nov 19 at 11:20
1
1
@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22
@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22
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?
– nixon
Nov 19 at 11:24
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?
– nixon
Nov 19 at 11:24
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
Nov 19 at 11:33
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
Nov 19 at 11:33
add a comment |
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
Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32
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
Nov 21 at 13:53
add a comment |
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
Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32
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
Nov 21 at 13:53
add a comment |
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
Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32
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
Nov 21 at 13:53
add a comment |
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
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
edited Nov 21 at 8:32
WhatsThePoint
2,11342034
2,11342034
answered Nov 21 at 7:11
Anuprita
285
285
Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32
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
Nov 21 at 13:53
add a comment |
Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32
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
Nov 21 at 13:53
Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32
Can you provide a bit of explanation as to why you think this will help?
– WhatsThePoint
Nov 21 at 8:32
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
Nov 21 at 13:53
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
Nov 21 at 13:53
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53373446%2fadd-numpy-array-values-as-columns-to-dataframe-by-unique-integer%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
Your desired output is not even valid Python...
– Nils Werner
Nov 19 at 11:18
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
Nov 19 at 11:20
1
@JeffreyEly, please edit so that the expected output makes sense.
– Deepak Saini
Nov 19 at 11:22
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?
– nixon
Nov 19 at 11:24
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
Nov 19 at 11:33