How to create new column in dataframe from existing column using conditionsHow do I check whether a file exists without exceptions?How can I safely create a nested directory?How to sort a dataframe by multiple column(s)Selecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasHow to change the order of DataFrame columns?Delete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Journal standards vs. personal standards

Making a wall made from glass bricks

/etc/hosts not working

Losing queen and then winning the game

Two palindromes are not enough

Have any large aeroplanes been landed — safely and without damage — in locations that they could not be flown away from?

Could you fall off a planet if it was being accelerated by engines?

Closest Proximity of Oceans to Freshwater Springs

Compiling all Exception messages into a string

How to describe POV characters?

Why was p[:] designed to work differently in these two situations?

Ways to get SMD resistors from a strip

Can European countries bypass the EU and make their own individual trade deal with the U.S.?

Do home values typically rise and fall consistently across different price ranges?

Conference in Los Angeles, visa?

13th chords on guitar

List Manipulation : a,b,c,d,e,f,g,h into a,b,c,d,e,f,g,h

Sharing referee/AE report online to point out a grievous error in refereeing

Checkmate in 1 on a Tangled Board

Cooking a nice pan seared steak for picky eaters

Adjective for 'made of pus' or 'corrupted by pus' or something of something of pus

Why did the Apple //e make a hideous noise if you inserted the disk upside down?

How do ohm meters measure high resistances?

Could human civilization live 150 years in a nuclear-powered aircraft carrier colony without resorting to mass killing/ cannibalism?



How to create new column in dataframe from existing column using conditions


How do I check whether a file exists without exceptions?How can I safely create a nested directory?How to sort a dataframe by multiple column(s)Selecting multiple columns in a pandas dataframeAdding new column to existing DataFrame in Python pandasHow to change the order of DataFrame columns?Delete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








11















I have one column containing all the data which looks something like this (values that need to be separated have a mark like (c)):



UK (c)
London
Wales
Liverpool
US (c)
Chicago
New York
San Francisco
Seattle
Australia (c)
Sydney
Perth


And I want it split into two columns looking like this:



London UK
Wales UK
Liverpool UK
Chicago US
New York US
San Francisco US
Seattle US
Sydney Australia
Perth Australia


Q2. What if the countries did not have a pattern like (c)?










share|improve this question









New contributor



Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    11















    I have one column containing all the data which looks something like this (values that need to be separated have a mark like (c)):



    UK (c)
    London
    Wales
    Liverpool
    US (c)
    Chicago
    New York
    San Francisco
    Seattle
    Australia (c)
    Sydney
    Perth


    And I want it split into two columns looking like this:



    London UK
    Wales UK
    Liverpool UK
    Chicago US
    New York US
    San Francisco US
    Seattle US
    Sydney Australia
    Perth Australia


    Q2. What if the countries did not have a pattern like (c)?










    share|improve this question









    New contributor



    Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      11












      11








      11








      I have one column containing all the data which looks something like this (values that need to be separated have a mark like (c)):



      UK (c)
      London
      Wales
      Liverpool
      US (c)
      Chicago
      New York
      San Francisco
      Seattle
      Australia (c)
      Sydney
      Perth


      And I want it split into two columns looking like this:



      London UK
      Wales UK
      Liverpool UK
      Chicago US
      New York US
      San Francisco US
      Seattle US
      Sydney Australia
      Perth Australia


      Q2. What if the countries did not have a pattern like (c)?










      share|improve this question









      New contributor



      Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have one column containing all the data which looks something like this (values that need to be separated have a mark like (c)):



      UK (c)
      London
      Wales
      Liverpool
      US (c)
      Chicago
      New York
      San Francisco
      Seattle
      Australia (c)
      Sydney
      Perth


      And I want it split into two columns looking like this:



      London UK
      Wales UK
      Liverpool UK
      Chicago US
      New York US
      San Francisco US
      Seattle US
      Sydney Australia
      Perth Australia


      Q2. What if the countries did not have a pattern like (c)?







      python pandas dataframe series






      share|improve this question









      New contributor



      Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question









      New contributor



      Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question








      edited 7 hours ago







      Tsatsa













      New contributor



      Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 8 hours ago









      TsatsaTsatsa

      585 bronze badges




      585 bronze badges




      New contributor



      Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      Tsatsa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          5 Answers
          5






          active

          oldest

          votes


















          5














          Step by step with endswith and ffill + str.strip



          df['country']=df.loc[df.city.str.endswith('(c)'),'city']
          df.country=df.country.ffill()
          df=df[df.city.ne(df.country)]
          df.country=df.country.str.strip('(c)')





          share|improve this answer

























          • What if the countries did not have a pattern like (c)?

            – Tsatsa
            7 hours ago






          • 1





            @Tsatsa in that case you may need build a country list , and using isin

            – WeNYoBen
            7 hours ago


















          5















          extract and ffill



          Start with extract and ffill, then remove redundant rows.



          df['country'] = (
          df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill())
          df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

          data country
          0 London UK
          1 Wales UK
          2 Liverpool UK
          3 Chicago US
          4 New York US
          5 San Francisco US
          6 Seattle US
          7 Sydney Australia
          8 Perth Australia


          Where,



          df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill()

          0 UK
          1 UK
          2 UK
          3 UK
          4 US
          5 US
          6 US
          7 US
          8 US
          9 Australia
          10 Australia
          11 Australia
          Name: country, dtype: object


          The pattern '(.*)s+(c)' matches strings of the form "country (c)" and extracts the country name. Anything not matching this pattern is replaced with NaN, so you can conveniently forward fill on rows.





          split with np.where and ffill



          This splits on "(c)".



          u = df['data'].str.split(r's+(c)')
          df['country'] = pd.Series(np.where(u.str.len() == 2, u.str[0], np.nan)).ffill()

          df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

          data country
          0 London UK
          1 Wales UK
          2 Liverpool UK
          3 Chicago US
          4 New York US
          5 San Francisco US
          6 Seattle US
          7 Sydney Australia
          8 Perth Australia





          share|improve this answer

























          • extract('(.*)s+(c)') saves you from .str.strip().

            – Quang Hoang
            8 hours ago











          • @QuangHoang Yes, it works. Thanks!

            – cs95
            8 hours ago


















          5














          You can first use str.extract to locate the cities ending in (c) and extract the country name, and ffill to populate a new country column.



          The same extracted matches can be use to locate the rows to be dropped, i.e. rows which are notna:



          m = df.city.str.extract('^(.*?)(?=(c)$)')
          ix = m[m.squeeze().notna()].index
          df['country'] = m.ffill()
          df.drop(ix)

          city country
          1 London UK
          2 Wales UK
          3 Liverpool UK
          5 Chicago US
          6 New York US
          7 San Francisco US
          8 Seattle US
          10 Sydney Australia
          11 Perth Australia





          share|improve this answer
































            4














            You can use np.where with str.contains too:



            mask = df['places'].str.contains('(c)', regex = False)
            df['country'] = np.where(mask, df['places'], np.nan)
            df['country'] = df['country'].str.replace('(c)', '').ffill()
            df = df[~mask]
            df
            places country
            1 London UK
            2 Wales UK
            3 Liverpool UK
            5 Chicago US
            6 New York US
            7 San Francisco US
            8 Seattle US
            10 Sydney Australia
            11 Perth Australia


            The str contains looks for (c) and if present will return True for that index. Where this condition is True, the country value will be added to the country columns






            share|improve this answer
































              2














              You could do the following:



              data = ['UK (c)','London','Wales','Liverpool','US (c)','Chicago','New York','San Francisco','Seattle','Australia (c)','Sydney','Perth']
              df = pd.DataFrame(data, columns = ['city'])
              df['country'] = df.city.apply(lambda x : x.replace('(c)','') if '(c)' in x else None)
              df.fillna(method='ffill', inplace=True)
              df = df[df['city'].str.contains('(c)')==False]


              Output



              +-----+----------------+-----------+
              | | city | country |
              +-----+----------------+-----------+
              | 1 | London | UK |
              | 2 | Wales | UK |
              | 3 | Liverpool | UK |
              | 5 | Chicago | US |
              | 6 | New York | US |
              | 7 | San Francisco | US |
              | 8 | Seattle | US |
              | 10 | Sydney | Australia |
              | 11 | Perth | Australia |
              +-----+----------------+-----------+





              share|improve this answer



























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



                );






                Tsatsa is a new contributor. Be nice, and check out our Code of Conduct.









                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56792723%2fhow-to-create-new-column-in-dataframe-from-existing-column-using-conditions%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                5














                Step by step with endswith and ffill + str.strip



                df['country']=df.loc[df.city.str.endswith('(c)'),'city']
                df.country=df.country.ffill()
                df=df[df.city.ne(df.country)]
                df.country=df.country.str.strip('(c)')





                share|improve this answer

























                • What if the countries did not have a pattern like (c)?

                  – Tsatsa
                  7 hours ago






                • 1





                  @Tsatsa in that case you may need build a country list , and using isin

                  – WeNYoBen
                  7 hours ago















                5














                Step by step with endswith and ffill + str.strip



                df['country']=df.loc[df.city.str.endswith('(c)'),'city']
                df.country=df.country.ffill()
                df=df[df.city.ne(df.country)]
                df.country=df.country.str.strip('(c)')





                share|improve this answer

























                • What if the countries did not have a pattern like (c)?

                  – Tsatsa
                  7 hours ago






                • 1





                  @Tsatsa in that case you may need build a country list , and using isin

                  – WeNYoBen
                  7 hours ago













                5












                5








                5







                Step by step with endswith and ffill + str.strip



                df['country']=df.loc[df.city.str.endswith('(c)'),'city']
                df.country=df.country.ffill()
                df=df[df.city.ne(df.country)]
                df.country=df.country.str.strip('(c)')





                share|improve this answer















                Step by step with endswith and ffill + str.strip



                df['country']=df.loc[df.city.str.endswith('(c)'),'city']
                df.country=df.country.ffill()
                df=df[df.city.ne(df.country)]
                df.country=df.country.str.strip('(c)')






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 8 hours ago

























                answered 8 hours ago









                WeNYoBenWeNYoBen

                143k8 gold badges51 silver badges79 bronze badges




                143k8 gold badges51 silver badges79 bronze badges












                • What if the countries did not have a pattern like (c)?

                  – Tsatsa
                  7 hours ago






                • 1





                  @Tsatsa in that case you may need build a country list , and using isin

                  – WeNYoBen
                  7 hours ago

















                • What if the countries did not have a pattern like (c)?

                  – Tsatsa
                  7 hours ago






                • 1





                  @Tsatsa in that case you may need build a country list , and using isin

                  – WeNYoBen
                  7 hours ago
















                What if the countries did not have a pattern like (c)?

                – Tsatsa
                7 hours ago





                What if the countries did not have a pattern like (c)?

                – Tsatsa
                7 hours ago




                1




                1





                @Tsatsa in that case you may need build a country list , and using isin

                – WeNYoBen
                7 hours ago





                @Tsatsa in that case you may need build a country list , and using isin

                – WeNYoBen
                7 hours ago













                5















                extract and ffill



                Start with extract and ffill, then remove redundant rows.



                df['country'] = (
                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill())
                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia


                Where,



                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill()

                0 UK
                1 UK
                2 UK
                3 UK
                4 US
                5 US
                6 US
                7 US
                8 US
                9 Australia
                10 Australia
                11 Australia
                Name: country, dtype: object


                The pattern '(.*)s+(c)' matches strings of the form "country (c)" and extracts the country name. Anything not matching this pattern is replaced with NaN, so you can conveniently forward fill on rows.





                split with np.where and ffill



                This splits on "(c)".



                u = df['data'].str.split(r's+(c)')
                df['country'] = pd.Series(np.where(u.str.len() == 2, u.str[0], np.nan)).ffill()

                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia





                share|improve this answer

























                • extract('(.*)s+(c)') saves you from .str.strip().

                  – Quang Hoang
                  8 hours ago











                • @QuangHoang Yes, it works. Thanks!

                  – cs95
                  8 hours ago















                5















                extract and ffill



                Start with extract and ffill, then remove redundant rows.



                df['country'] = (
                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill())
                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia


                Where,



                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill()

                0 UK
                1 UK
                2 UK
                3 UK
                4 US
                5 US
                6 US
                7 US
                8 US
                9 Australia
                10 Australia
                11 Australia
                Name: country, dtype: object


                The pattern '(.*)s+(c)' matches strings of the form "country (c)" and extracts the country name. Anything not matching this pattern is replaced with NaN, so you can conveniently forward fill on rows.





                split with np.where and ffill



                This splits on "(c)".



                u = df['data'].str.split(r's+(c)')
                df['country'] = pd.Series(np.where(u.str.len() == 2, u.str[0], np.nan)).ffill()

                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia





                share|improve this answer

























                • extract('(.*)s+(c)') saves you from .str.strip().

                  – Quang Hoang
                  8 hours ago











                • @QuangHoang Yes, it works. Thanks!

                  – cs95
                  8 hours ago













                5












                5








                5








                extract and ffill



                Start with extract and ffill, then remove redundant rows.



                df['country'] = (
                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill())
                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia


                Where,



                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill()

                0 UK
                1 UK
                2 UK
                3 UK
                4 US
                5 US
                6 US
                7 US
                8 US
                9 Australia
                10 Australia
                11 Australia
                Name: country, dtype: object


                The pattern '(.*)s+(c)' matches strings of the form "country (c)" and extracts the country name. Anything not matching this pattern is replaced with NaN, so you can conveniently forward fill on rows.





                split with np.where and ffill



                This splits on "(c)".



                u = df['data'].str.split(r's+(c)')
                df['country'] = pd.Series(np.where(u.str.len() == 2, u.str[0], np.nan)).ffill()

                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia





                share|improve this answer
















                extract and ffill



                Start with extract and ffill, then remove redundant rows.



                df['country'] = (
                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill())
                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia


                Where,



                df['data'].str.extract(r'(.*)s+(c)', expand=False).ffill()

                0 UK
                1 UK
                2 UK
                3 UK
                4 US
                5 US
                6 US
                7 US
                8 US
                9 Australia
                10 Australia
                11 Australia
                Name: country, dtype: object


                The pattern '(.*)s+(c)' matches strings of the form "country (c)" and extracts the country name. Anything not matching this pattern is replaced with NaN, so you can conveniently forward fill on rows.





                split with np.where and ffill



                This splits on "(c)".



                u = df['data'].str.split(r's+(c)')
                df['country'] = pd.Series(np.where(u.str.len() == 2, u.str[0], np.nan)).ffill()

                df[~df['data'].str.contains('(c)', regex=False)].reset_index(drop=True)

                data country
                0 London UK
                1 Wales UK
                2 Liverpool UK
                3 Chicago US
                4 New York US
                5 San Francisco US
                6 Seattle US
                7 Sydney Australia
                8 Perth Australia






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 8 hours ago

























                answered 8 hours ago









                cs95cs95

                156k26 gold badges208 silver badges278 bronze badges




                156k26 gold badges208 silver badges278 bronze badges












                • extract('(.*)s+(c)') saves you from .str.strip().

                  – Quang Hoang
                  8 hours ago











                • @QuangHoang Yes, it works. Thanks!

                  – cs95
                  8 hours ago

















                • extract('(.*)s+(c)') saves you from .str.strip().

                  – Quang Hoang
                  8 hours ago











                • @QuangHoang Yes, it works. Thanks!

                  – cs95
                  8 hours ago
















                extract('(.*)s+(c)') saves you from .str.strip().

                – Quang Hoang
                8 hours ago





                extract('(.*)s+(c)') saves you from .str.strip().

                – Quang Hoang
                8 hours ago













                @QuangHoang Yes, it works. Thanks!

                – cs95
                8 hours ago





                @QuangHoang Yes, it works. Thanks!

                – cs95
                8 hours ago











                5














                You can first use str.extract to locate the cities ending in (c) and extract the country name, and ffill to populate a new country column.



                The same extracted matches can be use to locate the rows to be dropped, i.e. rows which are notna:



                m = df.city.str.extract('^(.*?)(?=(c)$)')
                ix = m[m.squeeze().notna()].index
                df['country'] = m.ffill()
                df.drop(ix)

                city country
                1 London UK
                2 Wales UK
                3 Liverpool UK
                5 Chicago US
                6 New York US
                7 San Francisco US
                8 Seattle US
                10 Sydney Australia
                11 Perth Australia





                share|improve this answer





























                  5














                  You can first use str.extract to locate the cities ending in (c) and extract the country name, and ffill to populate a new country column.



                  The same extracted matches can be use to locate the rows to be dropped, i.e. rows which are notna:



                  m = df.city.str.extract('^(.*?)(?=(c)$)')
                  ix = m[m.squeeze().notna()].index
                  df['country'] = m.ffill()
                  df.drop(ix)

                  city country
                  1 London UK
                  2 Wales UK
                  3 Liverpool UK
                  5 Chicago US
                  6 New York US
                  7 San Francisco US
                  8 Seattle US
                  10 Sydney Australia
                  11 Perth Australia





                  share|improve this answer



























                    5












                    5








                    5







                    You can first use str.extract to locate the cities ending in (c) and extract the country name, and ffill to populate a new country column.



                    The same extracted matches can be use to locate the rows to be dropped, i.e. rows which are notna:



                    m = df.city.str.extract('^(.*?)(?=(c)$)')
                    ix = m[m.squeeze().notna()].index
                    df['country'] = m.ffill()
                    df.drop(ix)

                    city country
                    1 London UK
                    2 Wales UK
                    3 Liverpool UK
                    5 Chicago US
                    6 New York US
                    7 San Francisco US
                    8 Seattle US
                    10 Sydney Australia
                    11 Perth Australia





                    share|improve this answer















                    You can first use str.extract to locate the cities ending in (c) and extract the country name, and ffill to populate a new country column.



                    The same extracted matches can be use to locate the rows to be dropped, i.e. rows which are notna:



                    m = df.city.str.extract('^(.*?)(?=(c)$)')
                    ix = m[m.squeeze().notna()].index
                    df['country'] = m.ffill()
                    df.drop(ix)

                    city country
                    1 London UK
                    2 Wales UK
                    3 Liverpool UK
                    5 Chicago US
                    6 New York US
                    7 San Francisco US
                    8 Seattle US
                    10 Sydney Australia
                    11 Perth Australia






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 8 hours ago

























                    answered 8 hours ago









                    yatuyatu

                    26.4k4 gold badges22 silver badges53 bronze badges




                    26.4k4 gold badges22 silver badges53 bronze badges





















                        4














                        You can use np.where with str.contains too:



                        mask = df['places'].str.contains('(c)', regex = False)
                        df['country'] = np.where(mask, df['places'], np.nan)
                        df['country'] = df['country'].str.replace('(c)', '').ffill()
                        df = df[~mask]
                        df
                        places country
                        1 London UK
                        2 Wales UK
                        3 Liverpool UK
                        5 Chicago US
                        6 New York US
                        7 San Francisco US
                        8 Seattle US
                        10 Sydney Australia
                        11 Perth Australia


                        The str contains looks for (c) and if present will return True for that index. Where this condition is True, the country value will be added to the country columns






                        share|improve this answer





























                          4














                          You can use np.where with str.contains too:



                          mask = df['places'].str.contains('(c)', regex = False)
                          df['country'] = np.where(mask, df['places'], np.nan)
                          df['country'] = df['country'].str.replace('(c)', '').ffill()
                          df = df[~mask]
                          df
                          places country
                          1 London UK
                          2 Wales UK
                          3 Liverpool UK
                          5 Chicago US
                          6 New York US
                          7 San Francisco US
                          8 Seattle US
                          10 Sydney Australia
                          11 Perth Australia


                          The str contains looks for (c) and if present will return True for that index. Where this condition is True, the country value will be added to the country columns






                          share|improve this answer



























                            4












                            4








                            4







                            You can use np.where with str.contains too:



                            mask = df['places'].str.contains('(c)', regex = False)
                            df['country'] = np.where(mask, df['places'], np.nan)
                            df['country'] = df['country'].str.replace('(c)', '').ffill()
                            df = df[~mask]
                            df
                            places country
                            1 London UK
                            2 Wales UK
                            3 Liverpool UK
                            5 Chicago US
                            6 New York US
                            7 San Francisco US
                            8 Seattle US
                            10 Sydney Australia
                            11 Perth Australia


                            The str contains looks for (c) and if present will return True for that index. Where this condition is True, the country value will be added to the country columns






                            share|improve this answer















                            You can use np.where with str.contains too:



                            mask = df['places'].str.contains('(c)', regex = False)
                            df['country'] = np.where(mask, df['places'], np.nan)
                            df['country'] = df['country'].str.replace('(c)', '').ffill()
                            df = df[~mask]
                            df
                            places country
                            1 London UK
                            2 Wales UK
                            3 Liverpool UK
                            5 Chicago US
                            6 New York US
                            7 San Francisco US
                            8 Seattle US
                            10 Sydney Australia
                            11 Perth Australia


                            The str contains looks for (c) and if present will return True for that index. Where this condition is True, the country value will be added to the country columns







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 8 hours ago

























                            answered 8 hours ago









                            Mohit MotwaniMohit Motwani

                            3,0591 gold badge8 silver badges29 bronze badges




                            3,0591 gold badge8 silver badges29 bronze badges





















                                2














                                You could do the following:



                                data = ['UK (c)','London','Wales','Liverpool','US (c)','Chicago','New York','San Francisco','Seattle','Australia (c)','Sydney','Perth']
                                df = pd.DataFrame(data, columns = ['city'])
                                df['country'] = df.city.apply(lambda x : x.replace('(c)','') if '(c)' in x else None)
                                df.fillna(method='ffill', inplace=True)
                                df = df[df['city'].str.contains('(c)')==False]


                                Output



                                +-----+----------------+-----------+
                                | | city | country |
                                +-----+----------------+-----------+
                                | 1 | London | UK |
                                | 2 | Wales | UK |
                                | 3 | Liverpool | UK |
                                | 5 | Chicago | US |
                                | 6 | New York | US |
                                | 7 | San Francisco | US |
                                | 8 | Seattle | US |
                                | 10 | Sydney | Australia |
                                | 11 | Perth | Australia |
                                +-----+----------------+-----------+





                                share|improve this answer





























                                  2














                                  You could do the following:



                                  data = ['UK (c)','London','Wales','Liverpool','US (c)','Chicago','New York','San Francisco','Seattle','Australia (c)','Sydney','Perth']
                                  df = pd.DataFrame(data, columns = ['city'])
                                  df['country'] = df.city.apply(lambda x : x.replace('(c)','') if '(c)' in x else None)
                                  df.fillna(method='ffill', inplace=True)
                                  df = df[df['city'].str.contains('(c)')==False]


                                  Output



                                  +-----+----------------+-----------+
                                  | | city | country |
                                  +-----+----------------+-----------+
                                  | 1 | London | UK |
                                  | 2 | Wales | UK |
                                  | 3 | Liverpool | UK |
                                  | 5 | Chicago | US |
                                  | 6 | New York | US |
                                  | 7 | San Francisco | US |
                                  | 8 | Seattle | US |
                                  | 10 | Sydney | Australia |
                                  | 11 | Perth | Australia |
                                  +-----+----------------+-----------+





                                  share|improve this answer



























                                    2












                                    2








                                    2







                                    You could do the following:



                                    data = ['UK (c)','London','Wales','Liverpool','US (c)','Chicago','New York','San Francisco','Seattle','Australia (c)','Sydney','Perth']
                                    df = pd.DataFrame(data, columns = ['city'])
                                    df['country'] = df.city.apply(lambda x : x.replace('(c)','') if '(c)' in x else None)
                                    df.fillna(method='ffill', inplace=True)
                                    df = df[df['city'].str.contains('(c)')==False]


                                    Output



                                    +-----+----------------+-----------+
                                    | | city | country |
                                    +-----+----------------+-----------+
                                    | 1 | London | UK |
                                    | 2 | Wales | UK |
                                    | 3 | Liverpool | UK |
                                    | 5 | Chicago | US |
                                    | 6 | New York | US |
                                    | 7 | San Francisco | US |
                                    | 8 | Seattle | US |
                                    | 10 | Sydney | Australia |
                                    | 11 | Perth | Australia |
                                    +-----+----------------+-----------+





                                    share|improve this answer















                                    You could do the following:



                                    data = ['UK (c)','London','Wales','Liverpool','US (c)','Chicago','New York','San Francisco','Seattle','Australia (c)','Sydney','Perth']
                                    df = pd.DataFrame(data, columns = ['city'])
                                    df['country'] = df.city.apply(lambda x : x.replace('(c)','') if '(c)' in x else None)
                                    df.fillna(method='ffill', inplace=True)
                                    df = df[df['city'].str.contains('(c)')==False]


                                    Output



                                    +-----+----------------+-----------+
                                    | | city | country |
                                    +-----+----------------+-----------+
                                    | 1 | London | UK |
                                    | 2 | Wales | UK |
                                    | 3 | Liverpool | UK |
                                    | 5 | Chicago | US |
                                    | 6 | New York | US |
                                    | 7 | San Francisco | US |
                                    | 8 | Seattle | US |
                                    | 10 | Sydney | Australia |
                                    | 11 | Perth | Australia |
                                    +-----+----------------+-----------+






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 8 hours ago

























                                    answered 8 hours ago









                                    Sebastien DSebastien D

                                    2,1492 gold badges8 silver badges29 bronze badges




                                    2,1492 gold badges8 silver badges29 bronze badges




















                                        Tsatsa is a new contributor. Be nice, and check out our Code of Conduct.









                                        draft saved

                                        draft discarded


















                                        Tsatsa is a new contributor. Be nice, and check out our Code of Conduct.












                                        Tsatsa is a new contributor. Be nice, and check out our Code of Conduct.











                                        Tsatsa is a new contributor. Be nice, and check out our Code of Conduct.














                                        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%2f56792723%2fhow-to-create-new-column-in-dataframe-from-existing-column-using-conditions%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

                                        Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                        Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                        François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480