Is there any efficient way to concatenate Pandas column name, and don’t use loop. My current method is very slow. input : Output : Answer You could rework your dictionary to form groups and use groupby+agg(list): output:
Tag: python
Convert a recursive python code to a non-recursive version
The code provided here works unless we start to increase the distinct and n-symbols and length, for example, on my computer n_symbols=512, length=512, distinct=300 ends up with this error RecursionError: maximum recursion depth exceeded in comparison and then overflow errors if I increase the lru_cache value.…
Python Pandas Column Formation
Is there an easy way to reformat the columns from to Thanks Answer To reorder columns dynamically based on value, here is a way to do it: This returns a dataframe with columns (axis=1) ordered based on sorted value of first row. Here is a full example using your data sample:
Diophantine Equation
that takes a number (n) as an argument and returns tuple of four numbers which are; total number of packages, the number of packages of 6 nuggets, the number of packages of 9 nuggets and the number of packages of 20 nuggets that are needed to sell n number of nuggets. If the combination of nuggets cannot be m…
TypeError: object is not callable when instantiating during unit test
I have a class that throws an exception when instantiated with wrong values and I would like to write a unit test raising an exception when given wrong parameters. Instantiating an object outside of the self.assertRaises() doesn’t seem to cause any error and I fail to understand why it causes an error w…
Operation between 2 arrays for many rows based on date
I have a dataset df_1 that looks like this: date stock A stock B stock C stock D 2020-11-01 4 8 14 30 2020-11-10 0.4 0.6 0.8 0.2 2020-11-30 6 10 20 35 2020-12-01 6 10 20 35 2020-11-31 8 12 25 0.1 And a second dataset, df_2: date output1 output2 11/2020 stock A,stock B stock C, stock D 12/2020
Conditional call of a FastAPI Model
I have a multilang FastAPI connected to MongoDB. My document in MongoDB is duplicated in the two languages available and structured this way (simplified example): I therefore implemented two models DatasetFR and DatasetEN, each one makeS references with specific external Models (Enum) for category and tags in…
How to reverse a pandas series
I have a pandas series that must be flipped upside-down before I concatenate it to the main DataFrame. I can easily flip it with myseries = myseries.iloc[::-1] But when I attach it to the main DataFrame, it attaches the default series and not the flipped version. Why doesn’t the flipped series stay in p…
Find a Excel file in directory, compress and send it to another folder
I have an Excel file WK6 that is downloaded in the below folder: The Python script should first navigate till the above directory and then find the excel file WK6 (the name of the excel file changes as per week) and compress it. Then move it to some other directory. Please help me understand how can I find an…
How to use key in the sort function in this situation (Python)?
I have a list like this: Now I want to sort it, but by the number, not by the word. If I just use l.sort() it just sorts by alphabetically. My desired output should be sth like (d,0)(c,1)(a,2)(b,4) I tried l.sort(key = l[0][1]) but it won’t work too (l[0][1] actually refer to number 2, which is the seco…