I am a new python afficionado. For R users, there is one function : paste that helps to concatenate two or more variables in a dataframe. It's very useful. For example Suppose that I have this dataframe :
categorie titre tarifMin lieu long lat img dateSortie 1 zoo, Aquar 0.0 Aquar 2.385 48.89 ilo,0 2 zoo, Aquar 4.5 Aquar 2.408 48.83 ilo,0 6 lieu Jardi 0.0 Jardi 2.320 48.86 ilo,0 7 lieu Bois 0.0 Bois 2.455 48.82 ilo,0 13 espac Canal 0.0 Canal 2.366 48.87 ilo,0 14 espac Canal -1.0 Canal 2.384 48.89 ilo,0 15 parc Le Ma 20.0 Le Ma 2.353 48.87 ilo,0
I want to create a new column which uses another column in a dataframe and some text. With R, I do :
> y$thecolThatIWant=ifelse(y$tarifMin!=-1, + paste("Evenement permanent -->",y$categorie, + y$titre,"C partir de",y$tarifMin,"€uros"), + paste("Evenement permanent -->",y$categorie, + y$titre,"sans prix indique"))
And the result is :
> y categorie titre tarifMin lieu long lat img dateSortie 1 zoo, Aquar 0.0 Aquar 2.385 48.89 ilo,0 2 zoo, Aquar 4.5 Aquar 2.408 48.83 ilo,0 6 lieu Jardi 0.0 Jardi 2.320 48.86 ilo,0 7 lieu Bois 0.0 Bois 2.455 48.82 ilo,0 13 espac Canal 0.0 Canal 2.366 48.87 ilo,0 14 espac Canal -1.0 Canal 2.384 48.89 ilo,0 15 parc Le Ma 20.0 Le Ma 2.353 48.87 ilo,0 thecolThatIWant 1 Evenement permanent --> zoo, Aquar C partir de 0.0 €uros 2 Evenement permanent --> zoo, Aquar C partir de 4.5 €uros 6 Evenement permanent --> lieu Jardi C partir de 0.0 €uros 7 Evenement permanent --> lieu Bois C partir de 0.0 €uros 13 Evenement permanent --> espac Canal C partir de 0.0 €uros 14 Evenement permanent --> espac Canal C partir de -1.0 €uros 15 Evenement permanent --> parc Le Ma C partir de 20.0 €uros
My question is : How can I do the same thing in Python Pandas or some other module?
What I've tried so far: Well, I'm a very new user. So sorry for my mistake. I try to replicate the example in Python and we suppose that I get something like this
table=pd.read_csv("y.csv",sep=",") tt= table.loc[:,['categorie','titre','tarifMin','long','lat','lieu']] table ategorie titre tarifMin long lat lieu 0 zoo, Aquar 0.0 2.385 48.89 Aquar 1 zoo, Aquar 4.5 2.408 48.83 Aquar 2 lieu Jardi 0.0 2.320 48.86 Jardi 3 lieu Bois 0.0 2.455 48.82 Bois 4 espac Canal 0.0 2.366 48.87 Canal 5 espac Canal -1.0 2.384 48.89 Canal 6 parc Le Ma 20.0 2.353 48.87 Le Ma
I tried this basically
sc="Even permanent -->" + " "+ tt.titre+" "+tt.lieu tt['theColThatIWant'] = sc tt
And I got this
categorie titre tarifMin long lat lieu theColThatIWant 0 zoo, Aquar 0.0 2.385 48.89 Aquar Even permanent --> Aquar Aquar 1 zoo, Aquar 4.5 2.408 48.83 Aquar Even permanent --> Aquar Aquar 2 lieu Jardi 0.0 2.320 48.86 Jardi Even permanent --> Jardi Jardi 3 lieu Bois 0.0 2.455 48.82 Bois Even permanent --> Bois Bois 4 espac Canal 0.0 2.366 48.87 Canal Even permanent --> Canal Canal 5 espac Canal -1.0 2.384 48.89 Canal Even permanent --> Canal Canal 6 parc Le Ma 20.0 2.353 48.87 Le Ma Even permanent --> Le Ma Le Ma
Now, I suppose that I have to loop with condition if there is no vectorize like in R?
Project description Paste provides several pieces of “middleware” (or filters) that can be nested to build web applications. Each piece of middleware uses the WSGI (PEP 333) interface, and should be compatible with other middleware based on those interfaces.
paste() method in R programming is used to concatenate the two string values by separating with delimiters.
paste() in RThe paste() is a built-in R function used to concatenate vectors by converting them into character. The paste() method takes three parameters, and returns concatenated string. The paste0() function in R concatenates the vector without any separator. The paste() function concatenates the vectors or strings.
This very much works like Paste command in R: R code:
words = c("Here", "I","want","to","concatenate","words","using","pipe","delimeter") paste(words,collapse="|")
[1]
"Here|I|want|to|concatenate|words|using|pipe|delimeter"
Python:
words = ["Here", "I","want","to","concatenate","words","using","pipe","delimeter"] "|".join(words)
Result:
'Here|I|want|to|concatenate|words|using|pipe|delimeter'
Here's a simple implementation that works on lists, and probably other iterables. Warning: it's only been lightly tested, and only in Python 3.5+:
from functools import reduce def _reduce_concat(x, sep=""): return reduce(lambda x, y: str(x) + sep + str(y), x) def paste(*lists, sep=" ", collapse=None): result = map(lambda x: _reduce_concat(x, sep=sep), zip(*lists)) if collapse is not None: return _reduce_concat(result, sep=collapse) return list(result) assert paste([1,2,3], [11,12,13], sep=',') == ['1,11', '2,12', '3,13'] assert paste([1,2,3], [11,12,13], sep=',', collapse=";") == '1,11;2,12;3,13'
You can also have some more fun and replicate other functions like paste0
:
from functools import partial paste0 = partial(paste, sep="")
Edit: here's a Repl.it project with type-annotated versions of this code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With