logo
down
shadow

Select columns based on column name and location in Pandas


Select columns based on column name and location in Pandas

By : Vikram K S
Date : November 23 2020, 03:01 PM
will help you I have a dataframe with following columns for example: column names = ID, name, dob, away_dur, away_count, in_dur, in_time etc. , Use a list comprehension to filter out columns. Then, use df.tail.
code :
c = [x for x in df.columns if not x.startswith('in_')]
df = df[c].tail(-3)


Share : facebook icon twitter icon
how to select/add a column to pandas dataframe based on a function of other columns?

how to select/add a column to pandas dataframe based on a function of other columns?


By : user3012384
Date : March 29 2020, 07:55 AM
hop of those help? A vectorised solution using isin:
code :
In [5]:

L=[4,20,30]
df['Match'] = (df['A']*df['C']).isin(L)
df
Out[5]:
   A  B   C  Match
0  1  6   4   True
1  2  1  10   True
2  3  8   3  False
3  4  4   5   True
4  5  2   4   True
5  6  4   5   True
6  7  4   3  False
7  8  7   7  False
8  9  4   1  False
In [9]:

%%timeit
L=[4,20,30]
rowindex = df.apply(lambda x : True if (x['A'] * x['C']) in L else False, axis=1)
df.loc[rowindex,'match'] = True
df.loc[~rowindex,'match'] = False
100 loops, best of 3: 3.13 ms per loop
In [11]:

%%timeit 
L=[4,20,30]
df['Match'] = (df['A']*df['C']).isin(L)

1000 loops, best of 3: 678 µs per loop
How to conditionally select column based on other columns under pandas DataFrame without using where function?

How to conditionally select column based on other columns under pandas DataFrame without using where function?


By : user7517671
Date : March 29 2020, 07:55 AM
Hope that helps IIUC you can use numpy.where():
code :
In [120]: df['idMax'] =  \
              np.where(df["max"]<=abs(df["min"]),
                       df["idCaseMin"],
                       df["idCaseMax"])

In [121]: df
Out[121]:
        idCaseMax  idCaseMin  max  min  idMax
lineId
1               5         10  120 -110      5
2              27         23  150 -205     23
3              15         40  110  -80     15
4              11          8   90 -150      8
Select dataframe columns based on column values in Pandas

Select dataframe columns based on column values in Pandas


By : Karthika.Kandasamy
Date : March 29 2020, 07:55 AM
wish of those help My dataframe looks like: , Use:
code :
df = df.loc[:, df.mean() > 10]
print (df)
    C   D   Z
0  12  14   2
1  15  10  30
2  20  12  15
print (df.mean())
A     1.666667
B     7.000000
C    15.666667
D    12.000000
Y     3.333333
Z    15.666667
dtype: float64

print (df.mean() > 10)
A    False
B    False
C     True
D     True
Y    False
Z     True
dtype: bool
print (df[df.columns[df.mean() > 10]])
    C   D   Z
0  12  14   2
1  15  10  30
2  20  12  15
print (df.columns[df.mean() > 10])
Index(['C', 'D', 'Z'], dtype='object')
Select next column based on label reference to columns of pandas dataframe

Select next column based on label reference to columns of pandas dataframe


By : Czadd
Date : March 29 2020, 07:55 AM
around this issue You can use get_indexer() for this to get the index of the sublist and add 1 to get the next column index, then use df.iloc[]:
code :
df1.iloc[:,df1.columns.get_indexer(sublist)+1]
   qww12  hdbh
0     48    91
1     86    67
2     21    98
3     11    94
4      9    14
5     70    54
6     38    61
7     99    65
8     16    86
9     15    40
Select last row from each column of multi-index Pandas DataFrame based on time, when columns are unequal length

Select last row from each column of multi-index Pandas DataFrame based on time, when columns are unequal length


By : user3640039
Date : March 29 2020, 07:55 AM
Any of those help I have the following Pandas multi-index DataFrame with the top level index being a group ID and the second level index being when, in ISO 8601 time format (shown here without the time): , Where original DataFrame given in the question is df:
code :
import pandas as pd

df.sort_index(inplace=True)
result = df.loc[pd.IndexSlice[:, :when], :].groupby('id').tail(1)
result['age'] =  when - result.index.get_level_values(level=1)
Related Posts Related Posts :
  • How to calculate each single element of a numpy array based on conditions
  • How do I change the width of Jupyter notebook's cell's left part?
  • Measure distance between lat/lon coordinates and utm coordinates
  • Installing megam for NLTK on Windows
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org