Equivalent of Paste R to Python

后端 未结 8 699
忘掉有多难
忘掉有多难 2021-02-01 14:32

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

8条回答
  •  别那么骄傲
    2021-02-01 15:29

    This is simple example how to achive that (If I'am not worng what do you want to do):

    import numpy as np
    import pandas as pd
    
    dates = pd.date_range('20130101',periods=6)
    df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))
    for row in df.itertuples():
        index, A, B, C, D = row
        print '%s Evenement permanent  --> %s , next data %s' % (index, A, B)
    

    Output:

    >>>df
                       A         B         C         D
    2013-01-01 -0.400550 -0.204032 -0.954237  0.019025
    2013-01-02  0.509040 -0.611699  1.065862  0.034486
    2013-01-03  0.366230  0.805068 -0.144129 -0.912942
    2013-01-04  1.381278 -1.783794  0.835435 -0.140371
    2013-01-05  1.140866  2.755003 -0.940519 -2.425671
    2013-01-06 -0.610569 -0.282952  0.111293 -0.108521
    

    This what loop for print: 2013-01-01 00:00:00 Evenement permanent --> -0.400550121168 , next data -0.204032344442

    2013-01-02 00:00:00 Evenement permanent  --> 0.509040318928 , next data -0.611698560541
    
    2013-01-03 00:00:00 Evenement permanent  --> 0.366230438863 , next data 0.805067758304
    
    2013-01-04 00:00:00 Evenement permanent  --> 1.38127775713 , next data -1.78379439485
    
    2013-01-05 00:00:00 Evenement permanent  --> 1.14086631509 , next data 2.75500268167
    
    2013-01-06 00:00:00 Evenement permanent  --> -0.610568516983 , next data -0.282952162792
    

提交回复
热议问题