Ad
Select Rows From Dataframe And Include Another Column Python
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
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad