concatenating a new column and renaming it












1















I'm trying to generate a new column based on another (hour column from the time column). The problem is that after concatenating, it's getting the same name ("time").
Moreover, when I try to change one, the other changes as well.



Why is that?



Here's the code



df['time'] = pd.to_datetime(df['time'])
hour_col = pd.Series(df['time']).copy()
hour_col = hour_col.apply(lambda t: t.hour)
df = pd.concat([df, hour_col], axis=1)


Name change:



df = df.rename(columns={ df.columns[3]: "hour" })









share|improve this question



























    1















    I'm trying to generate a new column based on another (hour column from the time column). The problem is that after concatenating, it's getting the same name ("time").
    Moreover, when I try to change one, the other changes as well.



    Why is that?



    Here's the code



    df['time'] = pd.to_datetime(df['time'])
    hour_col = pd.Series(df['time']).copy()
    hour_col = hour_col.apply(lambda t: t.hour)
    df = pd.concat([df, hour_col], axis=1)


    Name change:



    df = df.rename(columns={ df.columns[3]: "hour" })









    share|improve this question

























      1












      1








      1








      I'm trying to generate a new column based on another (hour column from the time column). The problem is that after concatenating, it's getting the same name ("time").
      Moreover, when I try to change one, the other changes as well.



      Why is that?



      Here's the code



      df['time'] = pd.to_datetime(df['time'])
      hour_col = pd.Series(df['time']).copy()
      hour_col = hour_col.apply(lambda t: t.hour)
      df = pd.concat([df, hour_col], axis=1)


      Name change:



      df = df.rename(columns={ df.columns[3]: "hour" })









      share|improve this question














      I'm trying to generate a new column based on another (hour column from the time column). The problem is that after concatenating, it's getting the same name ("time").
      Moreover, when I try to change one, the other changes as well.



      Why is that?



      Here's the code



      df['time'] = pd.to_datetime(df['time'])
      hour_col = pd.Series(df['time']).copy()
      hour_col = hour_col.apply(lambda t: t.hour)
      df = pd.concat([df, hour_col], axis=1)


      Name change:



      df = df.rename(columns={ df.columns[3]: "hour" })






      python-3.x pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 13:19









      yasecoyaseco

      42413




      42413
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Use dt.hour:



          df['time'] = pd.to_datetime(df['time'])
          df['hours'] = df['time'].dt.hour




          But if really need your solution only rename column, converting to Series is not necessary, because each column of DataFrame is Series after selecting (print (type(df['time']))):



          df = pd.DataFrame({'time':['10:20:30','20:03:04']})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time'].rename('hour')
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)
          print (df)
          time hour
          0 2018-11-24 10:20:30 10
          1 2018-11-24 20:03:04 20


          If want replace columns by position (e.g. because duplicated values and rename change both names like mentioned @Gla Avineri in comment) use:



          df = pd.DataFrame({'time':['10:20:30','20:03:04'],
          'a':[2,3],
          'b':[-4,5]})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time']
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)

          #converting to list because columns are immutable
          cols = df.columns.tolist()
          #set 4th value
          cols[3] = 'hour'
          #assign back
          df.columns = cols

          print (df)
          time a b hour
          0 2018-11-24 10:20:30 2 -4 10
          1 2018-11-24 20:03:04 3 5 20





          share|improve this answer


























          • I'll try that, thanks. Out of curiosity, what is the problem with my code? Why is it behaving like that?

            – yaseco
            Nov 24 '18 at 13:23











          • I would guess that in your code, when you copy the time series of the dataframe, you are also copying it's name - "time". Than when you concatenate the new series to the dataframe, it appears with the name "time". When you use rename, you ask to rename df.columns[3] to "hour". But df.columns[3] holds the value "time", so what you basically wrote there is rename(columns={"time": "hour"}). As i explained earlier your df had 2 columns named "time" up to this point, so now they will both be changed to "hour".

            – Gal Avineri
            Nov 24 '18 at 14:13











          • @GalAvineri - Yes, I agree - Added solution for this.

            – jezrael
            Nov 24 '18 at 14:58











          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%2f53458550%2fconcatenating-a-new-column-and-renaming-it%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









          2














          Use dt.hour:



          df['time'] = pd.to_datetime(df['time'])
          df['hours'] = df['time'].dt.hour




          But if really need your solution only rename column, converting to Series is not necessary, because each column of DataFrame is Series after selecting (print (type(df['time']))):



          df = pd.DataFrame({'time':['10:20:30','20:03:04']})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time'].rename('hour')
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)
          print (df)
          time hour
          0 2018-11-24 10:20:30 10
          1 2018-11-24 20:03:04 20


          If want replace columns by position (e.g. because duplicated values and rename change both names like mentioned @Gla Avineri in comment) use:



          df = pd.DataFrame({'time':['10:20:30','20:03:04'],
          'a':[2,3],
          'b':[-4,5]})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time']
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)

          #converting to list because columns are immutable
          cols = df.columns.tolist()
          #set 4th value
          cols[3] = 'hour'
          #assign back
          df.columns = cols

          print (df)
          time a b hour
          0 2018-11-24 10:20:30 2 -4 10
          1 2018-11-24 20:03:04 3 5 20





          share|improve this answer


























          • I'll try that, thanks. Out of curiosity, what is the problem with my code? Why is it behaving like that?

            – yaseco
            Nov 24 '18 at 13:23











          • I would guess that in your code, when you copy the time series of the dataframe, you are also copying it's name - "time". Than when you concatenate the new series to the dataframe, it appears with the name "time". When you use rename, you ask to rename df.columns[3] to "hour". But df.columns[3] holds the value "time", so what you basically wrote there is rename(columns={"time": "hour"}). As i explained earlier your df had 2 columns named "time" up to this point, so now they will both be changed to "hour".

            – Gal Avineri
            Nov 24 '18 at 14:13











          • @GalAvineri - Yes, I agree - Added solution for this.

            – jezrael
            Nov 24 '18 at 14:58
















          2














          Use dt.hour:



          df['time'] = pd.to_datetime(df['time'])
          df['hours'] = df['time'].dt.hour




          But if really need your solution only rename column, converting to Series is not necessary, because each column of DataFrame is Series after selecting (print (type(df['time']))):



          df = pd.DataFrame({'time':['10:20:30','20:03:04']})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time'].rename('hour')
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)
          print (df)
          time hour
          0 2018-11-24 10:20:30 10
          1 2018-11-24 20:03:04 20


          If want replace columns by position (e.g. because duplicated values and rename change both names like mentioned @Gla Avineri in comment) use:



          df = pd.DataFrame({'time':['10:20:30','20:03:04'],
          'a':[2,3],
          'b':[-4,5]})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time']
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)

          #converting to list because columns are immutable
          cols = df.columns.tolist()
          #set 4th value
          cols[3] = 'hour'
          #assign back
          df.columns = cols

          print (df)
          time a b hour
          0 2018-11-24 10:20:30 2 -4 10
          1 2018-11-24 20:03:04 3 5 20





          share|improve this answer


























          • I'll try that, thanks. Out of curiosity, what is the problem with my code? Why is it behaving like that?

            – yaseco
            Nov 24 '18 at 13:23











          • I would guess that in your code, when you copy the time series of the dataframe, you are also copying it's name - "time". Than when you concatenate the new series to the dataframe, it appears with the name "time". When you use rename, you ask to rename df.columns[3] to "hour". But df.columns[3] holds the value "time", so what you basically wrote there is rename(columns={"time": "hour"}). As i explained earlier your df had 2 columns named "time" up to this point, so now they will both be changed to "hour".

            – Gal Avineri
            Nov 24 '18 at 14:13











          • @GalAvineri - Yes, I agree - Added solution for this.

            – jezrael
            Nov 24 '18 at 14:58














          2












          2








          2







          Use dt.hour:



          df['time'] = pd.to_datetime(df['time'])
          df['hours'] = df['time'].dt.hour




          But if really need your solution only rename column, converting to Series is not necessary, because each column of DataFrame is Series after selecting (print (type(df['time']))):



          df = pd.DataFrame({'time':['10:20:30','20:03:04']})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time'].rename('hour')
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)
          print (df)
          time hour
          0 2018-11-24 10:20:30 10
          1 2018-11-24 20:03:04 20


          If want replace columns by position (e.g. because duplicated values and rename change both names like mentioned @Gla Avineri in comment) use:



          df = pd.DataFrame({'time':['10:20:30','20:03:04'],
          'a':[2,3],
          'b':[-4,5]})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time']
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)

          #converting to list because columns are immutable
          cols = df.columns.tolist()
          #set 4th value
          cols[3] = 'hour'
          #assign back
          df.columns = cols

          print (df)
          time a b hour
          0 2018-11-24 10:20:30 2 -4 10
          1 2018-11-24 20:03:04 3 5 20





          share|improve this answer















          Use dt.hour:



          df['time'] = pd.to_datetime(df['time'])
          df['hours'] = df['time'].dt.hour




          But if really need your solution only rename column, converting to Series is not necessary, because each column of DataFrame is Series after selecting (print (type(df['time']))):



          df = pd.DataFrame({'time':['10:20:30','20:03:04']})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time'].rename('hour')
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)
          print (df)
          time hour
          0 2018-11-24 10:20:30 10
          1 2018-11-24 20:03:04 20


          If want replace columns by position (e.g. because duplicated values and rename change both names like mentioned @Gla Avineri in comment) use:



          df = pd.DataFrame({'time':['10:20:30','20:03:04'],
          'a':[2,3],
          'b':[-4,5]})

          df['time'] = pd.to_datetime(df['time'])
          hour_col = df['time']
          hour_col = hour_col.apply(lambda t: t.hour)
          df = pd.concat([df, hour_col], axis=1)

          #converting to list because columns are immutable
          cols = df.columns.tolist()
          #set 4th value
          cols[3] = 'hour'
          #assign back
          df.columns = cols

          print (df)
          time a b hour
          0 2018-11-24 10:20:30 2 -4 10
          1 2018-11-24 20:03:04 3 5 20






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 14:57

























          answered Nov 24 '18 at 13:21









          jezraeljezrael

          341k25296368




          341k25296368













          • I'll try that, thanks. Out of curiosity, what is the problem with my code? Why is it behaving like that?

            – yaseco
            Nov 24 '18 at 13:23











          • I would guess that in your code, when you copy the time series of the dataframe, you are also copying it's name - "time". Than when you concatenate the new series to the dataframe, it appears with the name "time". When you use rename, you ask to rename df.columns[3] to "hour". But df.columns[3] holds the value "time", so what you basically wrote there is rename(columns={"time": "hour"}). As i explained earlier your df had 2 columns named "time" up to this point, so now they will both be changed to "hour".

            – Gal Avineri
            Nov 24 '18 at 14:13











          • @GalAvineri - Yes, I agree - Added solution for this.

            – jezrael
            Nov 24 '18 at 14:58



















          • I'll try that, thanks. Out of curiosity, what is the problem with my code? Why is it behaving like that?

            – yaseco
            Nov 24 '18 at 13:23











          • I would guess that in your code, when you copy the time series of the dataframe, you are also copying it's name - "time". Than when you concatenate the new series to the dataframe, it appears with the name "time". When you use rename, you ask to rename df.columns[3] to "hour". But df.columns[3] holds the value "time", so what you basically wrote there is rename(columns={"time": "hour"}). As i explained earlier your df had 2 columns named "time" up to this point, so now they will both be changed to "hour".

            – Gal Avineri
            Nov 24 '18 at 14:13











          • @GalAvineri - Yes, I agree - Added solution for this.

            – jezrael
            Nov 24 '18 at 14:58

















          I'll try that, thanks. Out of curiosity, what is the problem with my code? Why is it behaving like that?

          – yaseco
          Nov 24 '18 at 13:23





          I'll try that, thanks. Out of curiosity, what is the problem with my code? Why is it behaving like that?

          – yaseco
          Nov 24 '18 at 13:23













          I would guess that in your code, when you copy the time series of the dataframe, you are also copying it's name - "time". Than when you concatenate the new series to the dataframe, it appears with the name "time". When you use rename, you ask to rename df.columns[3] to "hour". But df.columns[3] holds the value "time", so what you basically wrote there is rename(columns={"time": "hour"}). As i explained earlier your df had 2 columns named "time" up to this point, so now they will both be changed to "hour".

          – Gal Avineri
          Nov 24 '18 at 14:13





          I would guess that in your code, when you copy the time series of the dataframe, you are also copying it's name - "time". Than when you concatenate the new series to the dataframe, it appears with the name "time". When you use rename, you ask to rename df.columns[3] to "hour". But df.columns[3] holds the value "time", so what you basically wrote there is rename(columns={"time": "hour"}). As i explained earlier your df had 2 columns named "time" up to this point, so now they will both be changed to "hour".

          – Gal Avineri
          Nov 24 '18 at 14:13













          @GalAvineri - Yes, I agree - Added solution for this.

          – jezrael
          Nov 24 '18 at 14:58





          @GalAvineri - Yes, I agree - Added solution for this.

          – jezrael
          Nov 24 '18 at 14:58




















          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%2f53458550%2fconcatenating-a-new-column-and-renaming-it%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

          Wiesbaden

          Marschland

          Dieringhausen