renaming and selecting columns simultaneously in pandas

  • Last Update :
  • Techknowledgy :

The shortest answer to "how to add a range to select" is to pass the list of selected columns to the resultant dataframe of your rename operation:

df.rename(columns = {
   "Commander": "Com",
   "Score": "Sco"
})[['Com', 'Sco']]

Com Sco
Cochice Jason 4
Pima Molly 24
Santa Cruz Tina 31
Maricopa Jake 2
Yuma Amy 3

But it's a little tedious to rewrite the column names, right? So you can initialize the rename with a dictionary:

selector_d = {
   'Commander': 'Com',
   'Score': 'Sco'
}

and pass that to the rename and select operations:

df.rename(columns = selector_d)[[ * selector_d.values()]]
Com Sco
Cochice Jason 4
Pima Molly 24
Santa Cruz Tina 31
Maricopa Jake 2
Yuma Amy 3

Actually, you don't need the double brackets to select the columns from selector_d.values(), as seen here:

df.rename(columns = selector_d)[[ * selector_d.values()]].equals(
   df.rename(columns = selector_d)[selector_d.values()]
)
True

Try this:

df.columns = ['Com', 'Date', 'Sco']

You can do it in the same way in python as you did in R, using datar:

>>> from datar.all import tibble, select, f
>>> 
>>> data = {'Commander': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
...         'Date': ['2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08'], 
...         'Score': [4, 24, 31, 2, 3]}
>>> 
>>> df = tibble(**data)
>>> df >> select(Com=f.Commander,Sco=f.Score)
       Com     Sco
  <object> <int64>
0    Jason       4
1    Molly      24
2     Tina      31
3     Jake       2
4      Amy       3

Suggestion : 2

jQuery Tutorial , Hire developers ,python pandas use previous calculated values

IIUC, you need:

df.loc[: , ['L', 'C2'] + [i
   for i in range(4, len(df.columns))
]]

Output:

             L C2 4 5 6
             week
             1 0.156464 0.197580 0.885015 0.991281 0.478843
             2 0.744064 0.082760 0.694133 0.487298 0.026765
             3 0.371953 0.015918 0.494651 0.965285 0.348584
             4 0.528609 0.287760 0.788897 0.664366 0.094318
             5 0.316789 0.211593 0.921653 0.005872 0.174702

The part you are looking for:

[i
   for i in range(4, len(df.columns))
]

After this, it is about adding the lists in Python:

['L', 'C2'] + [i
   for i in range(4, len(df.columns))
]

Alternatively, you could use .iloc:

df.iloc[: , np.r_[0, 3, 4: len(df.columns)]]

Suggestion : 3

May 19, 2021

Create a basic data frame and rename a column in pandas

# Creating a basic DataFrame
import pandas as pd

df = pd.DataFrame({
   'name': ['Aryan', 'Rohan', 'Riya', 'Yash', 'Siddhant'],
   'Tpye': [
      'Full-time Employee', 'Intern', 'Part-time Employee',
      'Part-time Employee', 'Full-time Employee'
   ],
   'Dept': ['Administration', 'Technical', 'Management', 'Technical', 'Technical'],
   'Salary': [20000, 5000, 10000, 10000, 20000]
})

df

Renaming a single column

# Using df.columns to view the column names of the DataFrame
print('Before:', df.columns)
# Using df.columns to view the column names of the DataFrame
print('Before:', df.columns)
#OUTPUT
#OUTPUT

The rename method can also be used to rename multiple columns in pandas.
The original column names are passed as the keys in the dictionary and the new column names are passed as the values to the corresponding keys in the dictionary.

df.rename(columns = {
      "name": "Name",
      "Tpye": "Type",
      "Dept": "Department"
   },
   inplace = True)

print('After:', df.columns)

Suggestion : 4

I've been trying to select columns and anycodings_python renaming it with pandas. In R's dplyr it is anycodings_python pretty straight forward but when it comes to anycodings_python pandas I could not find a way to do it.,why pandas is not straightforward and how anycodings_python can I do this the same thing with pandas ?,After this, it is about adding the lists anycodings_pandas in Python:,and had to select the columns are interested

For example

import numpy as np
import pandas as pd
np.random.seed(128)

df = pd.DataFrame(np.random.random((5, 7)), index = pd.Series(range(1, 6), name = "week"))
df

0 1 2...4 5 6
week...
   1 0.866637 0.263145 0.131408...0.238924 0.645475 0.790599
2 0.601442 0.334299 0.119428...0.109724 0.602113 0.251285
3 0.299681 0.517116 0.715203...0.102088 0.706524 0.985842
4 0.559079 0.592019 0.741931...0.125672 0.907613 0.573170
5 0.731224 0.301214 0.066628...0.133776 0.655933 0.423496

then trying to change some column names

df.rename(columns = {
   0: "L",
   1: "W",
   2: "C1",
   3: "C2"
}, inplace = True)

and had to select the columns are interested

df.loc[: , 'L', 'C2', 4: 6]

the expected output should look like

week L C2 4 5 6
1 0.8666370428503979 0.041593443747687364 0.23892433469051455 0.6454746004955415 0.7905993520222332
2 0.6014424381923764 0.30913305250605294 0.10972378522258008 0.6021133114626169 0.25128495916256977
3 0.2996812876134075 0.9314494030471506 0.1020881539666203 0.7065238642131539 0.9858423635165023
4 0.5590790688036144 0.8212812049578435 0.12567153920750518 0.9076131583950552 0.5731702972368342
5 0.7312243453837555 0.16307047811396225 0.13377623506787528 0.6559325420882364 0.4234963284022535

IIUC, you need:

df.loc[: , ['L', 'C2'] + [i
   for i in range(4, len(df.columns))
]]

Output:

             L C2 4 5 6
             week
             1 0.156464 0.197580 0.885015 0.991281 0.478843
             2 0.744064 0.082760 0.694133 0.487298 0.026765
             3 0.371953 0.015918 0.494651 0.965285 0.348584
             4 0.528609 0.287760 0.788897 0.664366 0.094318
             5 0.316789 0.211593 0.921653 0.005872 0.174702

The part you are looking for:

[i
   for i in range(4, len(df.columns))
]

After this, it is about adding the lists anycodings_pandas in Python:

['L', 'C2'] + [i
   for i in range(4, len(df.columns))
]

Alternatively, you could use .iloc:

df.iloc[: , np.r_[0, 3, 4: len(df.columns)]]