determine maximum value of each row with the column name in Python - pandas
I have a table which's been attached.
The table includes a different number of model of cars with their EU class (columns are a number of models in each class).
I am trying to identify the maximum value of each model (at each row) by identifying the EU class (column name) in Python.
for example the first row the maximum number goes to Euro 5 with 3677 cars whose model of vehicle is 320 GH.
I tried different commands such as
maxValuesObj = D_high_model_EUstd.loc[D_high_model_EUstd['Model of Vehicle'].idxmax()]
but faced this error "reduction operation 'argmax' not allowed for this type".
I was wondering if anybody can help or suggest to me any solution.
Thanks
You can use pd.idxmax
For example:
df.drop(columns="Model of Vehicle").idxmax(axis=1) # Drop text column since idxmax don't work with them
Or create another dataframe:
pd.concat({
"Model of Vehicle": df["Model of Vehicle"],
"Max":df.drop(columns="Model of Vehicle").idxmax(axis=1)
},axis=1)
Related
Creating new column with new "other variable"
There are seven "Species" of fish in this data set, some have very few observations . To make analysis of Species a little easier, I need to create a new column in the data called Species.grouped that indicates if the fish is "Perch", "Bream", or "Other". So I have to group the 5 smallest groups of "Species" into a single group called "Other". The resultant column (Species.grouped) should have the value "Perch" if the fish is a Perch, "Bream" if it's a Bream, and "Other" if it's anything else. Then I need to run a regression predicting the Weight of a fish using Species.grouped and Width as independent predictor variables (no interaction).
Looks like a problem that could be solved by mapping 'Species' values to desired values/categories like 'Perch', 'Bream' and 'Other' using a dictionary and then applying that onto pandas. This Answer shows plenty of examples that could help you achieve your requirement.
How to populate all possible combination of values in columns, using Spark/normal SQL
I have a scenario, where my original dataset looks like below Data: Country,Commodity,Year,Type,Amount US,Vegetable,2010,Harvested,2.44 US,Vegetable,2010,Yield,15.8 US,Vegetable,2010,Production,6.48 US,Vegetable,2011,Harvested,6 US,Vegetable,2011,Yield,18 US,Vegetable,2011,Production,3 Argentina,Vegetable,2010,Harvested,15.2 Argentina,Vegetable,2010,Yield,40.5 Argentina,Vegetable,2010,Production,2.66 Argentina,Vegetable,2011,Harvested,15.2 Argentina,Vegetable,2011,Yield,40.5 Argentina,Vegetable,2011,Production,2.66 Bhutan,Vegetable,2010,Harvested,7 Bhutan,Vegetable,2010,Yield,35 Bhutan,Vegetable,2010,Production,5 Bhutan,Vegetable,2011,Harvested,2 Bhutan,Vegetable,2011,Yield,6 Bhutan,Vegetable,2011,Production,3 Image of the above csv: Now there is a very small country lookup table which has all possible countries the source data can come with, listed. PFB: I want to have the output data's number of columns always fixed (this is to ensure the reporting/visualization tool doesn't get dynamic number columns with every day's new source data ingestions depending on the varying distinct number of countries present). So, I've to somehow join the source data with the country_lookup csv and populate all those columns with default value as F. Every country column would be binary with T or F being the possible values. The original dataset from the above has to be converted into below: Data (I've kept the Amount field unsolved for column Type having Derived Yield as is, rather than calculating them below for a better understanding and for you to match with the formulae): Country,Commodity,Year,Type,Amount,US,Argentina,Bhutan,India,Nepal,Bangladesh US,Vegetable,2010,Harvested,2.44,T,F,F,F,F,F US,Vegetable,2010,Yield,15.8,T,F,F,F,F,F US,Vegetable,2010,Production,6.48,T,F,F,F,F,F US,Vegetable,2010,Derived Yield,(2.44+15.2)/(6.48+2.66),T,T,F,F,F,F US,Vegetable,2010,Derived Yield,(2.44+7)/(6.48+5),T,F,T,F,F,F US,Vegetable,2010,Derived Yield,(2.44+15.2+7)/(6.48+2.66+5),T,T,T,F,F,F US,Vegetable,2011,Harvested,6,T,F,F,F,F,F US,Vegetable,2011,Yield,18,T,F,F,F,F,F US,Vegetable,2011,Production,3,T,F,F,F,F,F US,Vegetable,2011,Derived Yield,(6+10)/(3+9),T,T,F,F,F,F US,Vegetable,2011,Derived Yield,(6+2)/(3+3),T,F,T,F,F,F US,Vegetable,2011,Derived Yield,(6+10+2)/(3+9+3),T,T,T,F,F,F Argentina,Vegetable,2010,Harvested,15.2,F,T,F,F,F,F Argentina,Vegetable,2010,Yield,40.5,F,T,F,F,F,F Argentina,Vegetable,2010,Production,2.66,F,T,F,F,F,F Argentina,Vegetable,2010,Derived Yield,(2.44+15.2)/(6.48+2.66),T,T,F,F,F,F Argentina,Vegetable,2010,Derived Yield,(15.2+7)/(2.66+5),F,T,T,F,F,F Argentina,Vegetable,2010,Derived Yield,(2.44+15.2+7)/(6.48+2.66+5),T,T,T,F,F,F Argentina,Vegetable,2011,Harvested,10,F,T,F,F,F,F Argentina,Vegetable,2011,Yield,90,F,T,F,F,F,F Argentina,Vegetable,2011,Production,9,F,T,F,F,F,F Argentina,Vegetable,2011,Derived Yield,(6+10)/(3+9),T,T,F,F,F,F Argentina,Vegetable,2011,Derived Yield,(10+2)/(9+3),F,T,T,F,F,F Argentina,Vegetable,2011,Derived Yield,(6+10+2)/(3+9+3),T,T,T,F,F,F Bhutan,Vegetable,2010,Harvested,7,F,F,T,F,F,F Bhutan,Vegetable,2010,Yield,35,F,F,T,F,F,F Bhutan,Vegetable,2010,Production,5,F,F,T,F,F,F Bhutan,Vegetable,2010,Derived Yield,(2.44+7)/(6.48+5),T,F,T,F,F,F Bhutan,Vegetable,2010,Derived Yield,(15.2+7)/(2.66+5),F,T,T,F,F,F Bhutan,Vegetable,2010,Derived Yield,(2.44+15.2+7)/(6.48+2.66+5),T,T,T,F,F,F Bhutan,Vegetable,2011,Harvested,2,F,F,T,F,F,F Bhutan,Vegetable,2011,Yield,6,F,F,T,F,F,F Bhutan,Vegetable,2011,Production,3,F,F,T,F,F,F Bhutan,Vegetable,2011,Derived Yield,(2.44+7)/(6.48+5),T,F,T,F,F,F Bhutan,Vegetable,2011,Derived Yield,(10+2)/(9+3),F,T,T,F,F,F Bhutan,Vegetable,2011,Derived Yield,(6+10+2)/(3+9+3),T,T,T,F,F,F The image of the above expected output data for a structured look at it: Part 1 - Part 2 - Formulae for populating Amount Field for Derived Type: Derived Amount = Sum of Harvested of all countries with T (True) grouped by Year and Commodity columns divided by Sum of Production of all countries with T (True)grouped by Year and Commodity columns. So, the target is to have a combination of all the countries from source and calculate the sum of respective Harvested and Production values which then has to be divided. The commodity can be more than one in the actual scenario for any given country, but that should not bother as the summation of amount happens on grouped commodity and year. Note: The users in the frontend can select any combination of countries. The sole purpose of doing it in the backend rather than dynamically doing it in the frontend is because AWS QuickSight (our visualisation tool), even though can populate sum on selected column filters but doesn't yet support calculation on those derived summed fields. Hence, the entire calculation of all combination of countries has to be pre-populated (very naive approach) in order to make it available in report on dynamic users selection of countries. Also if you've any better approach (than the above naive approach mentioned in note) to solve this problem, you are most welcome to guide me. I've also posted a question on the same problem without writing my expected approach for experts to show me the path on how we can solve this kind of a problem better than this naive approach. If you want to help solve it with some other technique, you're most welcome, here is the link to that question. Any help shall be greatly acknowledged.
Import data from csv into database when not all columns are guaranteed
I am trying to build an automatic feature for a database that takes NOAA weather data and imports it into our own database tables. Currently we have 3 steps: 1. Import the data literally into its own table to preserve the original data 2. Copy it's data into a table that better represents our own data in structure 3. Then convert that table into our own data The problem I am having stems from the data that NOAA gives us. It comes in the following format: Station Station_Name Elevation Latitude Longitude Date MXPN Measurement_Flag Quality_Flag Source_Flag Time_Of_Observation ... Starting with MXPN (Maximum temperature for water in a pan) which for example is comprised of it's column and the 4 other columns after it, it repeats that same 5 columns for each form of weather observation. The problem though is that if a particular type of weather was not observed in any of the stations reported, that set of 5 columns will be completely omitted. For example if you look at Central Florida stations, you will find no SNOW (Snowfall measured in mm). However, if you look at stations in New Jersey, you will find this column as they report snowfall. This means a 1:1 mapping of columns is not possible between different reports, and the order of columns may not be guaranteed. Even worse, some of the weather types include wild cards in their definition, e.g. SN*# where * is a number from 0-8 representing the type of ground, and # is a number 1-7 representing the depth at which soil temperature was taken for the minimum soil temperature, and we'd like to collect these together. All of these are column headers, and my instinct is to build a small Java program to map these properly to our data set as we'd like it. However, my superior believes it may be possible to have the database do this on a mass import, but he does not know how to do it. Is there a way to do this as a mass import, or is it best for me to just write the Java program to convert the data to our format? Systems in use: MariaDB for the database. Centos7 for the operating system (if it really becomes an issue) Java is being done with JPA and Spring Boot, with hibernate where necessary.
You are creating a new table per each file. I presume that the first 6 fields are always present, and that you have 0 or more occurrences of the next 5 fields. if you are using SQL Server i would approach it as follows, Query the information_schema catalog to get a count of the fields in the table. If the count= 6 then no observations are present, if 11 columns ,then you have 1 observation, if 17 then you have 2 observations, etc. Now that you know the number of observations you can write some SQL that will loop the over the observations and insert them into a child table with a link back to a parent table which has the 1st 6 fields. apologies if my assumptions are way off. -HTH
database design in django for 2-rows multiple colums
I am trying to create a model for the following data in django. I have around 20 columns and they can increase with time but I will only have one row for the values of each column which will update with time. What would be the best way of creating a model for such a data? I cannot transpose the rows into columns because the data is coming from another source which needs the database in the above format.
you can define a model say TheModel (a relevent name, may be). and each column would be one attribute. This way you can add more attributes as the column increases. According to your description, This model will have only one object. but that is fine right. class TheModel(models.Model): mda_table_ndx = models.CharField(max_length=200) mf_p030_.._.._.. = models.IntegerField() .............. ............. Let us know if there is a downside to this.
Two Dimensional Diagram with aggr function
I'm having a very curious Problem in QlikView. I have a number of readouts from a Database which show certain amounts of time in a different state. In that table there are 49 variables that describe the state, there are 7 levels of i.e the SOC and seven states of the Temperature. i.e the one of the fields could be named: SOC1_T1 or SOC2_T1 and so on... So what i get is a table full of readouts in which every i have an specific id for the object, the state of the variables and an age. There are multiple entries per Object. What i want to do is to plot a two dimensional diagram over all the states so i get SOC over Temperatur Histogram(Average of the maximum (or newest) value of every object). I tried creating to Dynamic (or syntethic) Dimensions (ValueLoop(1,7) and ValueLoop(1,8). In the formulas i reffered to them with =If(ValueLoop(1,7) = 1 and ValueLoop(1,8) = 1, (avg(aggr(FirstSortedValue (SOC1_T1 , -age), id)) * 100)) and created 49 Formulas with each state variable output. Problem now is: It only shows the first entry. I can replace the whole expression in the if condition with a specific number (100) and get a result. I also plotted the inner expression into a Listbox and checked wheter the result is not null. As soon as I delete the aggr function and just take the AVG over everything (which is not what i want). Everything works fine. When i turn back to aggr, only the first one is shown. Doesnt help by the way when i delete one of the dimensions, this doesnt work one dimensional either. Any ideas or workarounds? Greetings Julian