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
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
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 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
add a comment |
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
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
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
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
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
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
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
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 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
add a comment |
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
add a comment |
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
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