Select columns with name matching str with wildcard for t-test (Python)

Flora

I have

         Apple f2 m  Apple f2 t  Apple f3 m   Apple f3 t
0                 3           4           5            3
1                 12          7           4            7  
2                 5           9           7            5
3                 3           3           4            8
4                 7           1           2            6

I would like to select columns with str = 'Apple f* m' to do a t-test against columns with str = 'Apple f* t'

I have tried

ttest_ind(df.loc[:,df.columns.str.contains('Apple R* m')], df.loc[:,df.columns.str.contains('Apple R* t')]

However, it doesn't recognise my wildcard has a wildcard.

Thank you if you an help me solve or guide me for this problem.

Anton vBR

For future reference. The pandas.Series.str.contains has the param regex set to True by default which means we can use Regex expressions.

To find 0 or more of any character we can simply use this (ref. Alan Moore)

.* just means "0 or more of any character"

It's broken down into two parts:

. - a "dot" indicates any character * - means "0 or more instances of the preceding regex token"

Here is a link to regex101 where you can test regex expressions:

https://regex101.com/r/QNjkch/1

And finally we can simplify your code, consider this simple example:

import pandas as pd
df = pd.DataFrame(columns=["a1a","a2a","a1b"])

mask = df.columns.str.contains('a.*a')

df.loc[:,mask] # selects mask
df.loc[:,~mask] # selects inverted (by using ~) mask

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Matching strings with wildcard

From Dev

Is there a way to test for null on all columns in a wildcard select?

From Dev

python: MYSQLdb. how to get columns name without executing select * in a big table?

From Dev

SQL - select row with most matching columns

From Dev

Python - Using str.replace with a wildcard

From Dev

Joining with wildcard not matching strings

From Dev

Wildcard matching in Java

From Dev

Find the latest file(s) added to a git repository with name(s) matching a given wildcard pattern

From Dev

wildcard type matching - Java

From Dev

Select column of dataframe with name matching a string in Julia?

From Dev

Wildcard file matching in XML

From Dev

How to select DataFrame columns based on partial matching?

From Dev

Python: convention name for a test

From Dev

Multiply columns with rows by matching column name and row name in R

From Dev

Select columns matching names in a list

From Dev

Wildcard parameter in WebAPI routing matching where it shouldn't

From Dev

How to replace columns with matching name

From Dev

String Matching with wildcard in Python

From Dev

wildcard match & replace and/or multiple string wildcard matching

From Dev

Find the latest file(s) added to a git repository with name(s) matching a given wildcard pattern

From Dev

Select matching to two columns of subquery

From Dev

QRegularExpression wildcard matching

From Dev

Wildcard parameter in WebAPI routing matching where it shouldn't

From Dev

Define function to select columns by name, Dataframe Python

From Dev

Wildcard matching in Python

From Dev

Boolean & wildcard text matching

From Dev

RegEx Or with Wildcard not matching

From Dev

Can't set column name from index to str(index) + string (Pandas, Python)

From Dev

Python : T test ind looping over columns of df

Related Related

  1. 1

    Matching strings with wildcard

  2. 2

    Is there a way to test for null on all columns in a wildcard select?

  3. 3

    python: MYSQLdb. how to get columns name without executing select * in a big table?

  4. 4

    SQL - select row with most matching columns

  5. 5

    Python - Using str.replace with a wildcard

  6. 6

    Joining with wildcard not matching strings

  7. 7

    Wildcard matching in Java

  8. 8

    Find the latest file(s) added to a git repository with name(s) matching a given wildcard pattern

  9. 9

    wildcard type matching - Java

  10. 10

    Select column of dataframe with name matching a string in Julia?

  11. 11

    Wildcard file matching in XML

  12. 12

    How to select DataFrame columns based on partial matching?

  13. 13

    Python: convention name for a test

  14. 14

    Multiply columns with rows by matching column name and row name in R

  15. 15

    Select columns matching names in a list

  16. 16

    Wildcard parameter in WebAPI routing matching where it shouldn't

  17. 17

    How to replace columns with matching name

  18. 18

    String Matching with wildcard in Python

  19. 19

    wildcard match & replace and/or multiple string wildcard matching

  20. 20

    Find the latest file(s) added to a git repository with name(s) matching a given wildcard pattern

  21. 21

    Select matching to two columns of subquery

  22. 22

    QRegularExpression wildcard matching

  23. 23

    Wildcard parameter in WebAPI routing matching where it shouldn't

  24. 24

    Define function to select columns by name, Dataframe Python

  25. 25

    Wildcard matching in Python

  26. 26

    Boolean & wildcard text matching

  27. 27

    RegEx Or with Wildcard not matching

  28. 28

    Can't set column name from index to str(index) + string (Pandas, Python)

  29. 29

    Python : T test ind looping over columns of df

HotTag

Archive