相当于将R粘贴到Python [英] Equivalent of Paste R to Python
问题描述
我是一名新的python爱好者. 对于R用户,有一个功能:粘贴可帮助连接数据框中的两个或多个变量.非常有用 例如 假设我有这个数据框:
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
我想创建一个新列,该新列使用数据框中的另一列和一些文本.使用R,我可以做到:
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"))
结果是:
> 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
我的问题是:如何在Python Pandas或其他模块中执行相同的操作?
My question is : How can I do the same thing in Python Pandas or some other module?
到目前为止,我已经尝试过:我是一个非常新的用户.非常抱歉我的错误.我尝试在Python中复制示例,我们假设我得到了类似的东西
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
我基本上尝试过
sc="Even permanent -->" + " "+ tt.titre+" "+tt.lieu
tt['theColThatIWant'] = sc
tt
我明白了
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
现在,我想如果没有像R中那样的向量化,我必须循环条件?
Now, I suppose that I have to loop with condition if there is no vectorize like in R?
推荐答案
这非常类似于R中的Paste命令: R代码:
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)
结果:
在这里|我|想|要|连接|单词|使用|管道|定标"
'Here|I|want|to|concatenate|words|using|pipe|delimeter'
这篇关于相当于将R粘贴到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!