A simple question: Instead of expliciting categories every time in plotly express, like: Can plotly inherit categorical order already setted in pandas? like: So in the end will be just: Answer To my knowledge, the order of the categories is not automatically implemented for the x-axis. But you don’t hav…
Tag: pandas
How to search for name in dataframe
For example I want to find all the people that has “Abbott” in their name df.loc[name in df[“Name”]] I tried this and it didn’t work Answer You can use str.contains with the column you are interested in searching
How to make the frequency table based on the multiple columns in python?
I guess it could be a duplicated question, but I could not find the solution. I want to make a frequency table in python. And this is an expected result that is similar to the frequency table. I tried using crosstab, groupby, and pivot_table functions, but all of them failed to get the correct result. How can…
Transform dataframe format
How do I convert the following dataframe from df1 to df2? Thanks in advance! Answer You could try as follows. Apply .str.split to columns a and b in a loop, each time exploding the result. This will get us 2 pd.Series and we use pd.concat to put them together. After this, we just need to assign col c, and res…
sort values and create new column based on result
I have this example which produce this dataframe i want to sort values from column A and B and C from higher to lower and put result column headers in new column D like this: I hope it’s clear, thank you Answer You can try:
Merge two rows and put the results in the same columns
I would like to join two files by key but I would like some columns to be joined together for example: File1: File2: I would like to merge with primary key List (from file1) and Cod (from file2), to get: I think we need something like a left join and an agragation but I don’t know how. In the final
Python: need efficient way to set column value based on another column value
New Python user here, so I appreciate any ideas for best practices in addition to the issue I’m seeking advice on. I have code that works for a small number of records, but when I run it on a large dataframe, it takes too long. I’ve done a lot of reading on this issue, and there are several simila…
Select Rows Based on Time Difference [Before or After] In Columns
I have the following dataset of students taking 2 different exams: I want to select those students whose two exams are 10 days apart from each other in either direction. I am trying Timedelta, but I’m not sure if it’s optimal. Desired Output: Is there any better way of getting the desired output? …
Pandas DataFrame: How do I create numerical values out of numerical values from another column?
I have probably not explained my issue right in the headline, so let’s try to clarify it here. I want to categorise values from 1 column into a new one. The first ten lines in my data set are this: And the code I use is this I think this can be done easier with a user-defined function, but I
How can I map tuple key with df values updating an existing column?
I am trying to map a column of my df with a dictionary. My dictionary contains tuple as key and I want to update an existing column value based on the key. How can I achieve that ? sample df sample dict final df Answer Create Series with MultiIndex by keys, convert columns in same order like keys by DataFrame…