简体   繁体   中英

Can you simultaneously select and assign a column in a pandas DataFrame?

Using data.table in R, you can simultaneously select and assign columns. Assume one has a data.table with 3 columns--col1, col2, and col3. One could do the following using data.table:

dt2 <- dt[, .(col1, col2, newcol = 3, anothercol = col3)]

I want to do something similar in pandas but it looks like it would take 3 lines.

df2 = df.copy()
df2['newcol'] = 3
df2.rename(columns = {"col3" : "anothercol"})

Is there a more concise way to do what I did above?

This might work:

import pandas as pd

ddict = {
        'col1':['A','A','B','X'],
        'col2':['A','A','B','X'],
        'col3':['A','A','B','X'],
        }

df = pd.DataFrame(ddict)

df.loc[:, ['col1', 'col2', 'col3']].rename(columns={"col3":"anothercol"}).assign(newcol=3)

result:

  col1 col2 anothercol  newcol
0    A    A          A       3
1    A    A          A       3
2    B    B          B       3
3    X    X          X       3

I don't know R, but what I'm seeing is that you are adding a new column called newcol that has a value of 3 on all the rows.
also you are renaming a column from col3 to anothercol .
you don't really need do the copy step.

df2 = df.rename(columns = {'col3': 'anothercol'})
df2['newcol'] = 3

You can use df.assign for that :

Example :

>>> df = pd.DataFrame({'temp_c': [17.0, 25.0]},
                  index=['Portland', 'Berkeley'])

>>> df
          temp_c
Portland    17.0
Berkeley    25.0

>>> df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
          temp_c  temp_f
Portland    17.0    62.6
Berkeley    25.0    77.0

>>> df.assign(newcol=3).rename(columns={"temp_c":"anothercol"}
          anothercol  newcol
Portland        17.0       3
Berkeley        25.0       3

And then you can assign it to df2 . First examples taken from pandas Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM