logo
down
shadow

Easy method to move rows from df to another with coditions?


Easy method to move rows from df to another with coditions?

By : Anurag Bansal
Date : November 29 2020, 12:01 PM
it fixes the issue , Use the isin function:
code :
df = df[~df.Name.isin(ListOfNames)]


Share : facebook icon twitter icon
How to calculate sum from one column with some coditions in python pandas?

How to calculate sum from one column with some coditions in python pandas?


By : kamlesh nattamai ram
Date : March 29 2020, 07:55 AM
this one helps. I have one pandas Dataframe which looks like below: , Use:
code :
#convert column to int
df['person_count'] = df['person_count'].astype(int)

#reshape for one column from city and person_symbol columns 
df1=df.set_index(['sport_name','person_name','person_count']).stack().reset_index(name='val')
print (df1)
   sport_name person_name  person_count        level_3     val
0    football      ramesh            10           city  mumbai
1    football      ramesh            10  person_symbol     ram
2    football      ramesh            14           city  mumbai
3    football      ramesh            14  person_symbol     mum
4    football      ramesh            25           city   delhi
5    football      ramesh            25  person_symbol     mum
6    football      ramesh            20           city   delhi
7    football      ramesh            20  person_symbol     ram
8    football       mohit            11           city    pune
9    football       mohit            11  person_symbol     moh
10    cricket      mahesh            34           city   surat
11    cricket      mahesh            34  person_symbol     mah
12    cricket      mahesh            23           city   surat
13    cricket      mahesh            23  person_symbol     sur
14    cricket      mahesh            43           city   panji
15    cricket      mahesh            43  person_symbol     sur
16    cricket      mahesh            34           city   panji
17    cricket      mahesh            34  person_symbol     mah
#concatenate columns
a = df1['sport_name'] + '.' + df1['person_name'] + '.TOTAL.' + df1['val'] + '_count'

#groupby by Series a and aggregate sum
df2 = df1['person_count'].groupby(a.rename('derived_symbol'), sort=False)
                         .sum()
                         .reset_index(name='person_count')
print (df2)
                       derived_symbol  person_count
0  football.ramesh.TOTAL.mumbai_count            24
1     football.ramesh.TOTAL.ram_count            30
2     football.ramesh.TOTAL.mum_count            39
3   football.ramesh.TOTAL.delhi_count            45
4     football.mohit.TOTAL.pune_count            11
5      football.mohit.TOTAL.moh_count            11
6    cricket.mahesh.TOTAL.surat_count            57
7      cricket.mahesh.TOTAL.mah_count            68
8      cricket.mahesh.TOTAL.sur_count            66
9    cricket.mahesh.TOTAL.panji_count            77
how to get the record on the basis of certain coditions in sql

how to get the record on the basis of certain coditions in sql


By : KHAN
Date : March 29 2020, 07:55 AM
I wish this help you i need to fetch the record with some condtions from two table , You are missing GROUP BY clause, re-write the query
Find consecutive values of pandas with some coditions

Find consecutive values of pandas with some coditions


By : user2872828
Date : March 29 2020, 07:55 AM
wish helps you Here is main problem add next counter value by first non zero values by GroupBy.cumcount, but also use it for thresh, in my solution was added 1 to counter for distinguish first value in counter:
code :
g = df['Values'].ne(df['Values'].shift(1)).cumsum()
counts = df.groupby(['ID','Day',g])['Values'].cumcount() + 1
df['consec_zeros'] = np.where(df['Values'].eq(0), counts, 0)

#replace 0 to `NaN`s
a = df['consec_zeros'].mask(df['consec_zeros'].eq(0))
#add 1 to forward filling missing values by limit 1 per groups
df['consec_zeros'] = (np.where(a.isna(), 
                               a.groupby([df['ID'],df['Day']]).ffill(limit=1) + 1, 
                               df['consec_zeros']) - 1)
df['consec_zeros'] = df['consec_zeros'].fillna(0).astype(int)
print (df)
    Day  ID  Values  consec_zeros
0     1  aa     0.0             0
1     1  aa     0.0             1
2     1  aa     0.0             2
3     1  aa     0.0             3
4     1  aa     2.5             4
5     1  aa     2.3             0
6     1  aa     0.0             0
7     1  aa     0.0             1
8     1  aa     0.0             2
9     2  aa     0.0             0
10    2  aa     0.0             1
11    2  aa     2.3             2
12    2  aa     0.0             0
13    1  bb     0.0             0
14    1  bb     0.0             1
15    1  bb     0.0             2
16    1  bb     0.0             3
17    1  bb     3.5             4
mysqli query for multiple cases with coditions

mysqli query for multiple cases with coditions


By : user3116534
Date : March 29 2020, 07:55 AM
help you fix your problem Need to select data if condition first true then check user id exist, if condition not true then check condition two then check user id not exist , I think there is syntax error in your query. try this one
code :
SELECT * FROM tablename
 WHERE user_id = '1' 
    OR user_id IN('62', '63', '58', '6', '50', '19', '2', '17', '7', '3') 
    AND user_id NOT IN('35') 
    OR 
    (CASE 
        WHEN except_friends = 1 
            THEN NOT FIND_IN_SET('1', friends_list)
        WHEN selected_friends = 1 
            THEN NOT FIND_IN_SET('1', friends_list)
    END) ORDER BY id desc
How to skip the loops coditions for getting functionality

How to skip the loops coditions for getting functionality


By : user3834981
Date : March 29 2020, 07:55 AM
To fix the issue you can do I believe you are looking for continue.
Related Posts Related Posts :
  • Tuning the hyperparameter with gridsearch results in overfitting
  • some coordinates that I extracted from geocoder in Python are not saving in the variable I created
  • 7C in cs circles- python Im not sure what is wrong with this yet
  • How to fix 'AttributeError: 'list' object has no attribute 'shape'' error in python with Tensorflow / Keras when loading
  • python - thread`s target is a method of an object
  • Retrieve Variable From Class
  • What is the reason for matplotlib for printing labels multiple times?
  • Why would people use ThreadPoolExecutor instead of direct function call?
  • When clear_widgets is called, it doesnt remove screens in ScreenManager
  • Python can't import function
  • Pieces doesn't stack after one loop on my connect4
  • How to change font size of all .docx document with python-docx
  • How to store a word with # in .cfg file
  • How to append dictionaries to a dictionary?
  • How can I scrape text within paragraph tag with some other tags then within the paragraph text?
  • Custom entity ruler with SpaCy did not return a match
  • Logging with two handlers - one to file and one to stderr
  • How to do pivot_table in dask with aggfunc 'min'?
  • This for loop displays only the last entry of the student record
  • How to split a string by a specific pattern in number of characters?
  • Python 3: how to scrape research results from a website using CSFR?
  • Setting the scoring parameter of RandomizedSeachCV to r2
  • How to send alert or message from view.py to template?
  • How to add qml ScatterSeries to existing qml defined ChartView?
  • Django + tox: Apps aren't loaded yet
  • My css and images arent showing in django
  • Probability mass function sum 2 dice roll?
  • Cannot call ubuntu 'ulimit' from python subprocess without using shell option
  • Dataframe Timestamp Filter for new/repeating value
  • Problem with clicking select2 dropdownlist in selenium
  • pandas dataframe masks to write values into new column
  • How to click on item in navigation bar on top of page using selenium python?
  • Add multiple EntityRuler with spaCy (ValueError: 'entity_ruler' already exists in pipeline)
  • error when replacing missing ')' using negative look ahead regex in python
  • Is there a way to remove specific strings from indexes using a for loop?
  • select multiple tags by position in beautifulSoup
  • pytest: getting AttributeError: 'CaptureFixture' object has no attribute 'readouterror' capturing stdout
  • Shipping PyGObject/GTK+ app on Windows with MingW
  • Python script to deduplicate lines in multiple files
  • How to prevent window and widgets in a pyqt5 application from changing size when the visibility of one widget is altered
  • How to draw stacked bar plot from df.groupby('feature')['label'].value_counts()
  • Python subprocess doesn't work without sleep
  • How can I adjust 'the time' in python with module Re
  • Join original np array with resulting np array in a form of dictionary? multidimensional array? etc?
  • Forcing labels on histograms in each individual graph in a figure
  • For an infinite dataset, is the data used in each epoch the same?
  • Is there a more efficent way to extend a string?
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • shadow
    Privacy Policy - Terms - Contact Us © festivalmusicasacra.org