Remove blank spaces from a series

4

I have a list of this form, and I need to remove the spaces between words, that is, to make it look like this: 'jul07', 'jul08' ...

Here I leave the code:

    FechasList = []
    for i in date:
         if i not in FechasList:
                     FechasList.append(i)
    print FechasList
    [u'Jul 07', u'Jul 08', u'Jul 10', u'Jul 11', u'Jul 12', u'Jul 13']
    
asked by marixxza 13.07.2017 в 21:58
source

6 answers

3

Since at first you mention Pandas and the use of a "series", assuming that datos is a series of Pandas you can do the following:

fechas_lista = data.map(lambda x: x.replace(' ', '')).unique()

Note that this method considers equal "Jul 12" that "Jul12" or " Jul 12" and maintains the order of your series. Returns an array of NumPy.

Example:

import pandas as pd

data  = pd.Series([u'Jul 07', u'Jul 08', u'Jul 10', u'Jul 11',
                   u'Jul 12', u'Jul 13', u'Jul 07', u'Jul 08',
                   u'Jul 10', u'Jul 11', u' Jul 07', u'Jul08 '])

fechas_lista = data.map(lambda x: x.replace(' ', '')).unique()

Exit:

  

> > > dates_list
  array (['Jul07', 'Jul08', 'Jul10', 'Jul11', 'Jul12', 'Jul13'], dtype = object)

    
answered by 13.07.2017 / 23:00
source
3

You can try as follows:

import string
' hello  word'.translate(None, string.whitespace)

This causes all blank spaces in your string to be removed.

Greetings.

    
answered by 13.07.2017 в 22:18
3

Here I show you another way to do it using sets comprehension and the functions split() and join() , so you have variety:

fechas_lista = sorted({"".join(fecha.split()) for fecha in date})

When creating a set, the repeated elements disappear automatically. In this way, even in the case of having a date list as follows:

date = ['Jul 07', '  Jul  07', 'Jul 08', 'Jul 08  ']

The resulting list would not obtain repeated data because the set is about the dates with the spaces already removed, therefore the file automatically deletes elements repeated by the content, even if they have different blank spaces.

As the elements of the set do not have any predefined order, when calling sorted() passing it as an argument to the resulting set, you are sure of two things: you get a list and it is also ordered.

PD: Identifiers that start with a capital letter are often used to create classes or data types instead of variables.

    
answered by 13.07.2017 в 22:20
2

You can use the yourstring.replace(" ", "") function to remove spaces or if you only want those from the sides you can use yourstring.strip()

    
answered by 13.07.2017 в 22:04
2

You can remove the spaces as follows:

FechasList = []
for i in date:
    i = i.replace(' ', '')
    if i not in FechasList:
        FechasList.append(i)
print FechasList

Exit:

  

[u'Jul07 ', u'Jul08', u'Jul10 ', u'Jul11', u'Jul12 ', u'Jul13']

    
answered by 13.07.2017 в 22:01
1

You can do it with compressed lists:

FechaList = [f.replace(' ', '') for f in FechaList]
  

The advantage is that you can also do it lazily with iteretor , changing the [ by ( : (f.replace(' ', '') for f in FechaList) which is executed while it is traveling.

Or, you can also run a map with a function lambda to go through and convert the list to another using a function:

FechaList = map(lambda fecha: f.replace(' ', ''), FechaList)
  

Beware that map returns a map , to convert it to list you must do list(FechaList)

  

Miscellaneous: It should also be mentioned that for what you do above, to erase repeated, it is better if you use set , leaving something like list(set(date)) that is a list of unique values.

    
answered by 13.07.2017 в 22:15