Python Pandas Copying Columns












-1















I am trying to clean a dataframe using Pandas and I need to extract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts.



I need to do boolean indexing on the new columns after.



I tried creating a new column based off another column like this
hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.



This is what I currently have



enter image description here



This is what the data has to look like after



enter image description here



Thank you for any help



import os
import pandas as pd
from urllib.request import urlretrieve
url = "https://www.nhc.noaa.gov/data/hurdat/hurdat2-1851-2017-050118.txt"
local_fname = "hurdat2.txt"
if not os.path.exists("hurdat2.txt"):
urlretrieve(url, local_fname)


low_memory=False
hurricane_df = pd.read_csv("hurdat2.txt",engine='python',
delim_whitespace=True,names =
['date','time','record_id','status','latitude','longitude','max_wind',
'min_pressure','ne34ktr','se34ktr','sw34ktr','nw34ktr','ne50ktr','se50ktr',
'sw50ktr','nw50ktr','ne64ktr','se64ktr','sw64ktr','nw64ktr']
,header = None)

hurricane_df["identifier"] = hurricane_df["date"].copy()
hurricane_df[(hurricane_df['identifier'].str.contains('AL'))]


edit: What I ultimately want to do is toe xtract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts. So I started with taking the date column and adding those values into the identifier column and parsing the identifier column with strings that only begin with AL to get the identifier rows only.



What happened though was that the date column was still changing.



After I do that I want to fill that in with the tracking points, remove the rows with just the identifier information (that goes in the new date column which I would extract from the date as well, negating the AL(~) to get just the identifier information and then reorder the columns to the front of the dataframe (df[['c4','c5','c1','c2','c3']).










share|improve this question

























  • Please edit the question to include the code you have tried so far.

    – Kingsley
    Nov 15 '18 at 0:50











  • Added new code and what my ultimate goal is

    – Alex_777
    Nov 15 '18 at 1:45
















-1















I am trying to clean a dataframe using Pandas and I need to extract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts.



I need to do boolean indexing on the new columns after.



I tried creating a new column based off another column like this
hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.



This is what I currently have



enter image description here



This is what the data has to look like after



enter image description here



Thank you for any help



import os
import pandas as pd
from urllib.request import urlretrieve
url = "https://www.nhc.noaa.gov/data/hurdat/hurdat2-1851-2017-050118.txt"
local_fname = "hurdat2.txt"
if not os.path.exists("hurdat2.txt"):
urlretrieve(url, local_fname)


low_memory=False
hurricane_df = pd.read_csv("hurdat2.txt",engine='python',
delim_whitespace=True,names =
['date','time','record_id','status','latitude','longitude','max_wind',
'min_pressure','ne34ktr','se34ktr','sw34ktr','nw34ktr','ne50ktr','se50ktr',
'sw50ktr','nw50ktr','ne64ktr','se64ktr','sw64ktr','nw64ktr']
,header = None)

hurricane_df["identifier"] = hurricane_df["date"].copy()
hurricane_df[(hurricane_df['identifier'].str.contains('AL'))]


edit: What I ultimately want to do is toe xtract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts. So I started with taking the date column and adding those values into the identifier column and parsing the identifier column with strings that only begin with AL to get the identifier rows only.



What happened though was that the date column was still changing.



After I do that I want to fill that in with the tracking points, remove the rows with just the identifier information (that goes in the new date column which I would extract from the date as well, negating the AL(~) to get just the identifier information and then reorder the columns to the front of the dataframe (df[['c4','c5','c1','c2','c3']).










share|improve this question

























  • Please edit the question to include the code you have tried so far.

    – Kingsley
    Nov 15 '18 at 0:50











  • Added new code and what my ultimate goal is

    – Alex_777
    Nov 15 '18 at 1:45














-1












-1








-1


1






I am trying to clean a dataframe using Pandas and I need to extract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts.



I need to do boolean indexing on the new columns after.



I tried creating a new column based off another column like this
hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.



This is what I currently have



enter image description here



This is what the data has to look like after



enter image description here



Thank you for any help



import os
import pandas as pd
from urllib.request import urlretrieve
url = "https://www.nhc.noaa.gov/data/hurdat/hurdat2-1851-2017-050118.txt"
local_fname = "hurdat2.txt"
if not os.path.exists("hurdat2.txt"):
urlretrieve(url, local_fname)


low_memory=False
hurricane_df = pd.read_csv("hurdat2.txt",engine='python',
delim_whitespace=True,names =
['date','time','record_id','status','latitude','longitude','max_wind',
'min_pressure','ne34ktr','se34ktr','sw34ktr','nw34ktr','ne50ktr','se50ktr',
'sw50ktr','nw50ktr','ne64ktr','se64ktr','sw64ktr','nw64ktr']
,header = None)

hurricane_df["identifier"] = hurricane_df["date"].copy()
hurricane_df[(hurricane_df['identifier'].str.contains('AL'))]


edit: What I ultimately want to do is toe xtract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts. So I started with taking the date column and adding those values into the identifier column and parsing the identifier column with strings that only begin with AL to get the identifier rows only.



What happened though was that the date column was still changing.



After I do that I want to fill that in with the tracking points, remove the rows with just the identifier information (that goes in the new date column which I would extract from the date as well, negating the AL(~) to get just the identifier information and then reorder the columns to the front of the dataframe (df[['c4','c5','c1','c2','c3']).










share|improve this question
















I am trying to clean a dataframe using Pandas and I need to extract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts.



I need to do boolean indexing on the new columns after.



I tried creating a new column based off another column like this
hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.



This is what I currently have



enter image description here



This is what the data has to look like after



enter image description here



Thank you for any help



import os
import pandas as pd
from urllib.request import urlretrieve
url = "https://www.nhc.noaa.gov/data/hurdat/hurdat2-1851-2017-050118.txt"
local_fname = "hurdat2.txt"
if not os.path.exists("hurdat2.txt"):
urlretrieve(url, local_fname)


low_memory=False
hurricane_df = pd.read_csv("hurdat2.txt",engine='python',
delim_whitespace=True,names =
['date','time','record_id','status','latitude','longitude','max_wind',
'min_pressure','ne34ktr','se34ktr','sw34ktr','nw34ktr','ne50ktr','se50ktr',
'sw50ktr','nw50ktr','ne64ktr','se64ktr','sw64ktr','nw64ktr']
,header = None)

hurricane_df["identifier"] = hurricane_df["date"].copy()
hurricane_df[(hurricane_df['identifier'].str.contains('AL'))]


edit: What I ultimately want to do is toe xtract out those rows with the identifier, name, and number of points, and put them in new columns named named identifier, name, and num_pts. So I started with taking the date column and adding those values into the identifier column and parsing the identifier column with strings that only begin with AL to get the identifier rows only.



What happened though was that the date column was still changing.



After I do that I want to fill that in with the tracking points, remove the rows with just the identifier information (that goes in the new date column which I would extract from the date as well, negating the AL(~) to get just the identifier information and then reorder the columns to the front of the dataframe (df[['c4','c5','c1','c2','c3']).







python database pandas dataframe data-cleaning






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 1:44







Alex_777

















asked Nov 15 '18 at 0:35









Alex_777Alex_777

12




12













  • Please edit the question to include the code you have tried so far.

    – Kingsley
    Nov 15 '18 at 0:50











  • Added new code and what my ultimate goal is

    – Alex_777
    Nov 15 '18 at 1:45



















  • Please edit the question to include the code you have tried so far.

    – Kingsley
    Nov 15 '18 at 0:50











  • Added new code and what my ultimate goal is

    – Alex_777
    Nov 15 '18 at 1:45

















Please edit the question to include the code you have tried so far.

– Kingsley
Nov 15 '18 at 0:50





Please edit the question to include the code you have tried so far.

– Kingsley
Nov 15 '18 at 0:50













Added new code and what my ultimate goal is

– Alex_777
Nov 15 '18 at 1:45





Added new code and what my ultimate goal is

– Alex_777
Nov 15 '18 at 1:45












1 Answer
1






active

oldest

votes


















0














This only partially answers your question, but I hope it will be helpful:




I tried creating a new column based off another column like this hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.




To avoid this error, use



hurricane_df["new_column"] = hurricane_df["old_column"].copy()


In Python, doing variable_a = variable_b will not copy the value of variable_b and assign it to variable_a. It will just create a new name that is bound to the same object bound to variable_a.



For instance, if you do



a = 2
b = a
a = a + 1
print(b)


You'll get a 3. This is called "passing by reference"; other languages have "passing by value".



If you explain in more detail what your ultimate goal is we might find a way to help you (adding the rows it contains to a dataframe as new columns sounds a bit odd, and maybe there is a better way to do what you wanted to do in the first place).






share|improve this answer
























  • .copy() didnt work either

    – Alex_777
    Nov 16 '18 at 2:48











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310828%2fpython-pandas-copying-columns%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









0














This only partially answers your question, but I hope it will be helpful:




I tried creating a new column based off another column like this hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.




To avoid this error, use



hurricane_df["new_column"] = hurricane_df["old_column"].copy()


In Python, doing variable_a = variable_b will not copy the value of variable_b and assign it to variable_a. It will just create a new name that is bound to the same object bound to variable_a.



For instance, if you do



a = 2
b = a
a = a + 1
print(b)


You'll get a 3. This is called "passing by reference"; other languages have "passing by value".



If you explain in more detail what your ultimate goal is we might find a way to help you (adding the rows it contains to a dataframe as new columns sounds a bit odd, and maybe there is a better way to do what you wanted to do in the first place).






share|improve this answer
























  • .copy() didnt work either

    – Alex_777
    Nov 16 '18 at 2:48
















0














This only partially answers your question, but I hope it will be helpful:




I tried creating a new column based off another column like this hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.




To avoid this error, use



hurricane_df["new_column"] = hurricane_df["old_column"].copy()


In Python, doing variable_a = variable_b will not copy the value of variable_b and assign it to variable_a. It will just create a new name that is bound to the same object bound to variable_a.



For instance, if you do



a = 2
b = a
a = a + 1
print(b)


You'll get a 3. This is called "passing by reference"; other languages have "passing by value".



If you explain in more detail what your ultimate goal is we might find a way to help you (adding the rows it contains to a dataframe as new columns sounds a bit odd, and maybe there is a better way to do what you wanted to do in the first place).






share|improve this answer
























  • .copy() didnt work either

    – Alex_777
    Nov 16 '18 at 2:48














0












0








0







This only partially answers your question, but I hope it will be helpful:




I tried creating a new column based off another column like this hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.




To avoid this error, use



hurricane_df["new_column"] = hurricane_df["old_column"].copy()


In Python, doing variable_a = variable_b will not copy the value of variable_b and assign it to variable_a. It will just create a new name that is bound to the same object bound to variable_a.



For instance, if you do



a = 2
b = a
a = a + 1
print(b)


You'll get a 3. This is called "passing by reference"; other languages have "passing by value".



If you explain in more detail what your ultimate goal is we might find a way to help you (adding the rows it contains to a dataframe as new columns sounds a bit odd, and maybe there is a better way to do what you wanted to do in the first place).






share|improve this answer













This only partially answers your question, but I hope it will be helpful:




I tried creating a new column based off another column like this hurricane_df['new_col'] = hurricane_df['col'] but when trying to index the new_col it would also index the original col.




To avoid this error, use



hurricane_df["new_column"] = hurricane_df["old_column"].copy()


In Python, doing variable_a = variable_b will not copy the value of variable_b and assign it to variable_a. It will just create a new name that is bound to the same object bound to variable_a.



For instance, if you do



a = 2
b = a
a = a + 1
print(b)


You'll get a 3. This is called "passing by reference"; other languages have "passing by value".



If you explain in more detail what your ultimate goal is we might find a way to help you (adding the rows it contains to a dataframe as new columns sounds a bit odd, and maybe there is a better way to do what you wanted to do in the first place).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 1:24









KipirpoKipirpo

103




103













  • .copy() didnt work either

    – Alex_777
    Nov 16 '18 at 2:48



















  • .copy() didnt work either

    – Alex_777
    Nov 16 '18 at 2:48

















.copy() didnt work either

– Alex_777
Nov 16 '18 at 2:48





.copy() didnt work either

– Alex_777
Nov 16 '18 at 2:48




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310828%2fpython-pandas-copying-columns%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python