How to remove an index column from a derived dataframe? - python

I have a dataframe whose columns are derived from summary statistics of a data set. During the process it seems an index column is created that I can't get rid of. I describe the problem in detail in the screenshots below. Can you tell how to remove the seemingly index Financial Year without exporting the dataframe to Excel?
I should also note that attempt to drop index at the beginning doesn't work either as shown below

You could use the reset_index() method. More in: https://datagy.io/pandas-drop-index-column/

Related

How can I remove or drop multi indexes in a list from a dataframe in python without compromising the dataset?

I have tried using the .drop() func to remove the particular indexes from the dataframe but the remaining dataframe after the removal seems to have lost data in some columns. I'm puzzled as to why columns which used to have data displaying NaN.

How to check differences between column values in pandas?

I'm manually comparing two or three rows very similar using pandas. Is there a more automated way to do this? I would like a better method than using '=='.
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.diff.html
See if this will satisfy your needs.
df['sales_diff'] = df['sales'].diff()
The above code snippet creates a new column in your data frame, which contains the difference between the previous row by default. You can screw around with the parameters (axis) to compare rows or columns and you can change (period) to compare to a specific row or column.

Simple DataFrame question from a beginner

I'm learning dataframe now. I've been stuck in how to get a subset of a dataframe or table with its label index. I know it's a very simple question but I couldn't find the solution in pandas documentation. Hope someone could help me. Appreciate your help.
So, I have a dataframe named df_teams like below:
enter image description here
If I want to get a subtable of a specific team 'Warriors', I can use df_teams[df_teams['nickname']=='Warriors'], resulting a row in the form of dataframe. My question is, what if I want to get a subtable of more teams, say I want information of both 'Warriors' and 'Hawks' to form a new table? Can I do something similar by using logical index and finishing in one line of code?
You could do a bitwise or on the two conditions using the '|' character.
df_teams[(df_teams['nickname']=='Warriors')|(df_teams['nickname']=='Hawks')]
Alternatively if you have a list of values you want to check against you could instead use the isin method to return rows that have one of the values present in the list.
E.g
df_teams[df_teams['nickname'].isin(['Warriors','Hawks'])]

pd.groupby using specific Headers?

So Basically here is a simplified version of my dataframe, and the 2. picture is what I want to get. : https://imgur.com/a/44QgR44
An explanation: Basically the 20201001 stuff is the date in a number format, and I want to group up the values for each date for some Group and Name.
Here comes my issue: I tried using df.groupby(by=['Credit','Equity','Bond').sum but It grouped everything up, not only the ones in the list (there are much more in the original dataset which I dont want to group up.
The second issue is that there are 2 things which group up in a different row (Stock and Option) so not sure how I could do that with pandas.
In excel I just got the result by a simple SUMIF function.

Pandas and complicated filtering and merge/join multiple sub-data frames

I have a seemingly complicated problem and I have a general idea of how I should solve it but I am not sure if it is the best way to go about it. I'll give the scenario and would appreciate any help on how to break this down. I'm fairly new with Pandas so please excuse my ignorance.
The Scenario
I have a CSV file that I import as a dataframe. My example I am working through contains 2742 rows × 136 columns. The rows are variable but the columns are set. I have a set of 23 lookup tables (also as CSV files) named per year, per quarter (range is 2020 3rd quarter - 2015 1st quarter) The lookup files are named as such: PPRRVU203.csv. So that contains values from the 3rd quarter of 2020. The lookup tables are matched by two columns ('Code' and 'Mod') and I use three values that are associated in the lookup.
I am trying to filter sections of my data frame, pull the correct values from the matching lookup file, merge back into the original subset, and then replace into the original dataframe.
Thoughts
I can probably abstract this and wrap in a function but not sure how I can place back in. My question, for those that understand Pandas better than myself, what is the best method to filter, replace the values, and write the file back out.
The straight forward solution would be to filter the original dataframe into 23 separate dataframes, then do the merge on each individual file, then concat into a new dataframe and output to CSV.
This seems highly inefficient?
I can post code but I am looking for more of any high-level thoughts?
Not sure exactly how your DataFrame looks like but Pandas.query() method will maybe prove useful for the selection of data.
name = df.query('columnname == "something"')

Categories