Ad

Select Rows From Dataframe And Include Another Column Python

- 1 answer

I an new to python, How can I select columns from dataframe which end with _old and include B column as final output? Here is my dataframe:

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'james weker john mark jane der liv vam'.split(),
                   'C_old': np.arange(8), 'D_old': np.arange(8) * 2})
print(df1)

I have tried this which only selects columns which endswith _old but I what to include B column in the output

df1[df1.columns[pd.Series(df1.columns).str.endswith('_old')]]

My EXPECTED output should look like this

import pandas as pd
import numpy as np
df1 = pd.DataFrame({
                   'B': 'james weker john mark jane der liv vam'.split(),
                   'C_old': np.arange(8), 'D_old': np.arange(8) * 2})
print(df1)
Ad

Answer

Use DataFrame.filter with regex - $ for ends of string, | for or and ^B$ for select by column B (^ is for start of string)

df = df1.filter(regex='_old$|^B$')

You can select by masks - use DataFrame.loc for select all rows (:) and columns by condition:

df = df1.loc[:, df1.columns.str.endswith('_old') | (df1.columns == 'B')]

Or use Index.union for add B to columns:

df = df1[df1.columns[df1.columns.str.endswith('_old')].union(['B'])]
print (df)
       B  C_old  D_old
0  james      0      0
1  weker      1      2
2   john      2      4
3   mark      3      6
4   jane      4      8
5    der      5     10
6    liv      6     12
7    vam      7     14
Ad
source: stackoverflow.com
Ad