logo
down
shadow

Pandas Dataframe Filtering Columns and return column name


Pandas Dataframe Filtering Columns and return column name

By : Ivan Ulisses
Date : November 21 2020, 07:38 AM
seems to work fine You can filter the columns first to get the cols of interest and then call apply and use the boolean mask to mask the cols:
code :
In [18]:
cat_cols = df.columns[df.columns.str.contains('Cat')]
df[cat_cols].apply(lambda row: ', '.join(cat_cols[row == 1]), axis=1)

Out[18]:
0          Cat3
1    Cat1, Cat3
dtype: object
df[cat_cols].apply(lambda row: ', '.join(row.index[row == 1]), axis=1)


Share : facebook icon twitter icon
pandas dataframe: return column that is a compression of other columns

pandas dataframe: return column that is a compression of other columns


By : Dávid
Date : March 29 2020, 07:55 AM
may help you . Well, I'd probably do it as follows (an example dataframe the hopefully captures your situation well enough):
code :
>>> df

   A  B abc1 abc2 abc3 abc4
0  1  4    x    r    a    d
1  1  3    y    d    b    e
2  2  4    z    e    c    r
3  3  5    r    g    d    f
4  4  8    z    z    z    z
>>> cols = [x for x in df.columns if 'abc' in x]
>>> cols
['abc1', 'abc2', 'abc3', 'abc4']

>>> df['newcol'] = (df[cols] == 'r').any(axis=1).map({True:'r',False:'np.nan'})
>>> df

  A  B abc1 abc2 abc3 abc4  newcol
0  1  4    x    r    a    d       r
1  1  3    y    d    b    e  np.nan
2  2  4    z    e    c    r       r
3  3  5    r    g    d    f       r
4  4  8    z    z    z    z  np.nan
>>> df['newcol'] = (df[cols] == 'r').any(axis=1)
>>> df

   A  B abc1 abc2 abc3 abc4 newcol
0  1  4    x    r    a    d   True
1  1  3    y    d    b    e  False
2  2  4    z    e    c    r   True
3  3  5    r    g    d    f   True
4  4  8    z    z    z    z  False
>>> df

  A  B abc1  abc2 abc3 abc4
0  1  4    x  root    a    d
1  1  3    y     d    b    e
2  2  4    z     e    c  bar
3  3  5    r     g    d    f
4  4  8    z     z    z    z

>>> cols = [x for x in df.columns if 'abc' in x]
>>> df['newcol'] = df[cols].apply(lambda x: x.str.contains('r'),axis=0).any(axis=1)
>>> df['newcol'] = df['newcol'].map({True:'r',False:'np.nan'}) 
>>> df

   A  B abc1  abc2 abc3 abc4  newcol
0  1  4    x  root    a    d       r
1  1  3    y     d    b    e  np.nan
2  2  4    z     e    c  bar       r
3  3  5    r     g    d    f       r
4  4  8    z     z    z    z  np.nan
How to filter a pandas DataFrame for a certain column value and only return columns that do not have NAN?

How to filter a pandas DataFrame for a certain column value and only return columns that do not have NAN?


By : Asier
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Example data:
code :
dt[dt['year']==2001].dropna(axis=1)
In a pandas dataframe I would like to assign a value to a column based on filtering other columns to certain values

In a pandas dataframe I would like to assign a value to a column based on filtering other columns to certain values


By : Maxjo Uitzendkracht
Date : March 29 2020, 07:55 AM
wish helps you You're pretty close, but one of your conditions, that Pclass is both 1 and 2, is impossible, the syntax for intervals does not exist, and you'll want parentheses to separate each of your conditions:
code :
train.loc[(train['Sex'] == 'male') & ((train['Pclass'] == 1) | (train['Pclass'] == 2)) & (train['AgeGrp'] > 0) & (train['AgeGrp'] <= 5), 'ModelPrediction'] = 1
Convert Nx1 pandas dataframe with single 1xM array-containing column to M columns in Pandas dataframe

Convert Nx1 pandas dataframe with single 1xM array-containing column to M columns in Pandas dataframe


By : user3492373
Date : March 29 2020, 07:55 AM
With these it helps Another variation is to apply pd.Series to the column and massage the column names:
code :
df= pd.DataFrame( { "age": [[1,2,3,4],[2,3,4,5]] })

df = df["age"].apply(pd.Series)
df.columns = ["age1","age2","age3","age4"]
Filtering out certain columns in a pandas dataframe

Filtering out certain columns in a pandas dataframe


By : dominikgrygiel
Date : March 29 2020, 07:55 AM
I hope this helps . I am trying to filter out certain columns in my pandas dataframe based on the column name. , This should do it:
Related Posts Related Posts :
  • classification of data where attribute values are strings
  • Validate user input using regular expressions
  • Synchronizing and Resampling two timeseries with non-uniform millisecond intraday data
  • determing the number of sentences, words and letters in a text file
  • Deploying impure Python packages to AWS
  • Navigating between multiple Tkinter GUI frames
  • Python - Do I need to remove instances from a dictionary?
  • How can I get the edited values corresponding to the keys of a dictionary in views.py POST method, passed as a context v
  • differentiate between python function and class function
  • From array create tuples on if condition python
  • Looping over a text file list with python
  • Monitoring a real-time data stream with a flask web-app
  • Bad quality after multiple fade effect with pydub
  • delete rows in numpy array in python
  • What are the possible numpy value format strings?
  • Conditional Selecting of child elements in pdfquery
  • Python: split string by closing bracket and write in new line
  • SyntaxWarning: import * only allowed at module level
  • theano ~ use an index matrix and embeddings matrix to produce a 3D tensor?
  • Django background infinite loop process management
  • How can I use Pandas or Numpy to infer a datatype from a list of values?
  • How to add the sum of cubes using a function in python?
  • django registration redux URL's being effected by url with multiple query parameters
  • python - how can I generate a WAV file with beeps?
  • How can I implement a custom RNN (specifically an ESN) in Tensorflow?
  • Python modulo result differs from wolfram alpha?
  • Django on App Engine Managed VM
  • Python - CSV Reading with dictionary
  • Python script works in librarys examples folder, but not in parent directory
  • Dealing with Nested Loops in Python - Options?
  • Get indices of roughly equal sized chunks
  • python - creating dictionary from excel using specific columns
  • SQLAlchemy Determine If Unique Constraint Exists
  • Can I stop rendering variables in Django?
  • Scrapy: traversing a document
  • Common logger settings in Python logging dictConfig
  • Should I pass the object in or build it in the constructor?
  • 3d and 2d subplots in plotly
  • Apache Spark CombineByKey with list of elements in Python
  • How do I round up to the highest multiple of 10 in python?
  • ValueError: invalid literal for int() with base 10: 'skip'
  • How to get entire VARCHAR(MAX) column with Python pypyodbc
  • Use value of variable rather than keyword in python numpy.savez
  • Overlapping cron job that runs the same Django management command: problematic?
  • Distributed Powerset
  • Set a python variable to a C++ object pointer with boost-python
  • How to change array layout?
  • How do I properly structure Templates and include them in the settings in Django 1.8.6?
  • Function parameter semantics (with nested functions)
  • Not enough arguments for format string python
  • How to extract equation from a polynomial fit?
  • How to enable unicode for Python on Fedora?
  • How to check for a POST method in a ListView in Django views? I'm getting a 405 error
  • Pandas fillna values with numpy array
  • How to change the date format in a list of dates
  • Passing a variable between two test cases?
  • h2o: iterate through rows
  • scipy curve_fit fails on easy linear fit?
  • how to multiply dataframes elementwise and create multi-index -- Python
  • Can Django's development server route mutliple sites? If yes, how?
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co