Update rows of pandas dataframe based upon other rows - python
I have a pandas dataframe which has the following columns ( pk1, pk2 type, qty_6, qty_7 ). I have type as predicted_90, override_90, predicted_50, override 50. Now Based upon combination of pk1 and pk2 If for type predicted_50, predicted_90 contains some value for override_50, override_90 apart from NaN, I want to update my dataframe columns predicted_50, predicted_90 with override_50 and override_90 respectively. Also, I want to capture this change in a boolean column called qty_6_overridden, qty_7_overridden. Also, I want to capture the difference between the both in a column qty_6_dev, qty_7_dev.
qty_6_dev = qty_6 override - qty_6 predicted
Example dataframe :
data=[
['B01FV0FBX4','2019-01-13','predicted_90',2207.931,2217.841],
['B01FV0FBX4','2019-01-13','predicted_50',1561.033,1521.567],
['B01FV0FBX4','2019-01-13','override_90',1973.000,np.NaN],
['B01FV0FBX4','2019-01-13','override_50',1233.000,np.NaN],
['B01FV0FBX4','2019-01-06','override_50',np.NaN,1233.000],
['B01FV0FBX4','2019-01-06','predicted_50',1210.129,1213.803],
['B01FV0FBX4','2019-01-06','override_90',np.NaN,1973.000],
['B01FV0FBX4','2019-01-06','predicted_90',1911.205,1921.594]
]
df = pd.DataFrame(data,columns=['pk1','pk2', 'type', 'qty_6', 'qty_7'])
Expected output :
data=[
['B01FV0FBX4','2019-01-13','predicted_90',1973.000,2217.841,-234.931,0,True,False],
['B01FV0FBX4','2019-01-13','predicted_50',1233.000,1521.567,-328.033,0,True,False],
['B01FV0FBX4','2019-01-13','override_90',1973.000,np.NaN,0,0,False,False],
['B01FV0FBX4','2019-01-13','override_50',1233.000,np.NaN,0,0,False,False],
['B01FV0FBX4','2019-01-06','override_50',np.NaN,1233.000,0,0,False,False],
['B01FV0FBX4','2019-01-06','predicted_50',1210.129,1213.000,0,-0.803,False,True],
['B01FV0FBX4','2019-01-06','override_90',np.NaN,1973.000,0,0,False,False],
['B01FV0FBX4','2019-01-06','predicted_90',1911.205,1973.000,0,51.406,False,True]
]
df = pd.DataFrame(data,columns=['pk1','pk2', 'type', 'qty_6', 'qty_7','qty_6_dev','qty_7_dev', 'qty_6_overridden','qty_7_overridden'])
In the example you can see, the quantities with override exchange quantitties with predicted and we get the corresponding columns 'qty_6_dev','qty_7_dev', 'qty_6_overridden','qty_7_overridden'.
I was able to write a solution. It works but it looks horrible and very difficult to understand for others.
import pandas as pd
import numpy as np
import math
data=[
['B01FV0FBX4','2019-01-13','predicted_90',2207.931,2217.841],
['B01FV0FBX4','2019-01-13','predicted_50',1561.033,1521.567],
['B01FV0FBX4','2019-01-13','override_90',1973.000,np.NaN],
['B01FV0FBX4','2019-01-13','override_50',1233.000,np.NaN],
['B01FV0FBX4','2019-01-06','override_50',np.NaN,1233.000],
['B01FV0FBX4','2019-01-06','predicted_50',1210.129,1213.803],
['B01FV0FBX4','2019-01-06','override_90',np.NaN,1973.000],
['B01FV0FBX4','2019-01-06','predicted_90',1911.205,1921.594]
]
df = pd.DataFrame(data,columns=['pk1','pk2', 'type', 'qty_6', 'qty_7'])
override_map = {
"predicted_50" : "override_50",
"predicted_90" : "override_90"
}
def transform_df(df):
transformed_df = pd.DataFrame()
for index, row in df.iterrows():
row_type = row['type']
row_pk1 = row['pk1']
row_pk2 = row['pk2']
if row_type in override_map.keys():
override_type = override_map.get(row_type)
else:
for i in range(6,8):
qty_dev_col = 'qty_'+str(i)+'_dev'
qty_override_col = 'qty_'+str(i)+'_overridden'
row[qty_dev_col] = 0
row[qty_override_col] = False
transformed_df=transformed_df.append(row, ignore_index=True)
continue
corr_df = df.loc[(df.type == override_type)
& (df.pk1 == row_pk1)
& (df.pk2 == row_pk2)]
for i in range(6,8):
qty_col = 'qty_'+str(i)
qty_dev_col = 'qty_'+str(i)+'_dev'
qty_override_col = 'qty_'+str(i)+'_overridden'
if not (math.isnan(corr_df[qty_col])) and (corr_df[qty_col].values[0] != row[qty_col]):
row[qty_dev_col] = corr_df[qty_col].values[0] - row[qty_col]
row[qty_col] = corr_df[qty_col].values[0]
row[qty_override_col] = True
else:
row[qty_dev_col] = 0
row[qty_override_col] = False
transformed_df=transformed_df.append(row, ignore_index=True)
return transformed_df
x1 = transform_df(df)
Is there a better way to do this using lambdas or something ? Also this takes like forever to run over a bigger dataframe.
Related
Pandas include single row in df after filtering with .loc
So, in this function: def filter_by_freq(df, frequency): filtered_df = df.copy() if frequency.upper() == 'DAY': pass else: date_obj = filtered_df['Date'].values[0] target_day = pd.to_datetime(date_obj).day target_month = pd.to_datetime(date_obj).month final_date_obj = filtered_df['Date'].values[-1] if frequency.upper() == 'MONTH': filtered_df = filtered_df.loc[filtered_df['Date'].dt.day.eq(target_day)] elif frequency.upper() == 'YEAR': filtered_df = filtered_df.loc[filtered_df['Date'].dt.day.eq(target_day)] filtered_df = filtered_df.loc[filtered_df['Date'].dt.month.eq(target_month)] return filtered_df How can I also include in the .loc the very last row from the original df? Tried doing (for month frequency): filtered_df = filtered_df.loc[(filtered_df['Date'].dt.day.eq(target_day)) | (filtered_df['Date'].dt.date.eq(final_date_obj))] but didn't work. Thanks for your time!
Here's one way you could do it. In this example I have a df and I want to filter out all rows that have c1 > 0.5, but I want to keep the last row no matter what. I create a boolean series called lte_half to keep track of the first condition, and then I create another boolean series/list/array (all interchangeable) called end_ind which is True only for the last row. The filtered table is created by taking all rows that pass either condition with the | import pandas as pd import numpy as np np.random.seed(0) df = pd.DataFrame({'c1':np.random.rand(20)}) lte_half = df['c1'].le(0.5) end_ind = df.index == df.index[-1] filt_df = df[lte_half | end_ind] print(filt_df)
Split a dataframe into two dataframe using first column that have a string values in python
I have two .txt file where I want to separate the data frame into two parts using the first column value. If the value is less than "H1000", we want in a first dataframe and if it is greater or equal to "H1000" we want in a second dataframe.First column starts the value with H followed by a four numbers. I want to ignore H when comparing numbers less than 1000 or greater than 1000 in python. What I have tried this thing,but it is not working. ht_data = all_dfs.index[all_dfs.iloc[:, 0] == "H1000"][0] print(ht_data) Here is my code: if (".txt" in str(path_txt).lower()) and path_txt.is_file(): txt_files = [Path(path_txt)] else: txt_files = list(Path(path_txt).glob("*.txt")) for fn in txt_files: all_dfs = pd.read_csv(fn,sep="\t", header=None) #Reading file all_dfs = all_dfs.dropna(axis=1, how='all') #Drop the columns where all columns are NaN all_dfs = all_dfs.dropna(axis=0, how='all') #Drop the rows where all columns are NaN print(all_dfs) ht_data = all_dfs.index[all_dfs.iloc[:, 0] == "H1000"][0] print(ht_data) df_h = all_dfs[0:ht_data] # Head Data df_t = all_dfs[ht_data:] # Tene Data Can anyone help me how to achieve this task in python?
Assuming this data import pandas as pd data = pd.DataFrame( [ ["H0002", "Version", "5"], ["H0003", "Date_generated", "8-Aug-11"], ["H0004", "Reporting_period_end_date", "19-Jun-11"], ["H0005", "State", "AW"], ["H1000", "Tene_no/Combined_rept_no", "E75/3794"], ["H1001", "Tenem_holder Magnetic Resources", "NL"], ], columns = ["id", "col1", "col2"] ) We can create a mask of over and under a pre set threshold, like 1000. mask = data["id"].str.strip("H").astype(int) < 1000 df_h = data[mask] df_t = data[~mask]
If you want to compare values of the format val = HXXXX where X is a digit represented as a character, try this: val = 'H1003' val_cmp = int(val[1:]) if val_cmp < 1000: # First Dataframe else: # Second Dataframe
Python Pandas rolling mean DataFrame Constructor not properly called
I am trying to create a simple time-series, of different rolling types. One specific example, is a rolling mean of N periods using the Panda python package. I get the following error : ValueError: DataFrame constructor not properly called! Below is my code : def py_TA_MA(v, n, AscendType): df = pd.DataFrame(v, columns=['Close']) df = df.sort_index(ascending=AscendType) # ascending/descending flag M = pd.Series(df['Close'].rolling(n), name = 'MovingAverage_' + str(n)) df = df.join(M) df = df.sort_index(ascending=True) #need to double-check this return df Would anyone be able to advise? Kind regards
found the correction! It was erroring out (new error), where I had to explicitly declare n as an integer. Below, the code works #xw.func #xw.arg('n', numbers = int, doc = 'this is the rolling window') #xw.ret(expand='table') def py_TA_MA(v, n, AscendType): df = pd.DataFrame(v, columns=['Close']) df = df.sort_index(ascending=AscendType) # ascending/descending flag M = pd.Series(df['Close'], name = 'Moving Average').rolling(window = n).mean() #df = pd.Series(df['Close']).rolling(window = n).mean() df = df.join(M) df = df.sort_index(ascending=True) #need to double-check this return df
Pandas set element style dependent on another dataframe mith multi index
I have previously asked the question Pandas set element style dependent on another dataframe, which I have a working solution to, but now I am trying to apply it to a data frame with a multi index and I am getting an error, which I do not understand. Problem I have a pandas df and accompanying boolean matrix. I want to highlight the df depending on the boolean matrix. Data import pandas as pd import numpy as np from datetime import datetime date = pd.date_range(start = datetime(2016,1,1), end = datetime(2016,2,1), freq = "D") i = len(date) dic = {'X':pd.DataFrame(np.random.randn(i, 2),index = date, columns = ['A','B']), 'Y':pd.DataFrame(np.random.randn(i, 2),index = date, columns = ['A','B']), 'Z':pd.DataFrame(np.random.randn(i, 2),index = date, columns = ['A','B'])} df = pd.concat(dic.values(),axis=1,keys=dic.keys()) boo = [True, False] bool_matrix = {'X':pd.DataFrame(np.random.choice(boo, (i,2), p=[0.3,.7]), index = date, columns = ['A','B']), 'Y':pd.DataFrame(np.random.choice(boo, (i,2), p=[0.3,.7]), index = date, columns = ['A','B']), 'Z':pd.DataFrame(np.random.choice(boo, (i,2), p=[0.3,.7]), index = date, columns = ['A','B'])} bool_matrix =pd.concat(bool_matrix.values(),axis=1,keys=bool_matrix.keys()) My attempted solution def highlight(value): return 'background-color: green' my_style = df.style for column in df.columns: for i in df[column].index: data = bool_matrix.loc[i, column] if data: my_style = df.style.use(my_style.export()).applymap(highlight, subset = pd.IndexSlice[i, column]) my_style Results The above throws an AttributeError: 'Series' object has no attribute 'applymap' I do not understand what is returning as a Series. This is a single value I am subsetting and this solution worked for non multi-indexed df's as shown below. Without Multi-index import pandas as pd import numpy as np from datetime import datetime np.random.seed(24) date = pd.date_range(start = datetime(2016,1,1), end = datetime(2016,2,1), freq = "D") df = pd.DataFrame({'A': np.linspace(1, 100, len(date))}) df = pd.concat([df, pd.DataFrame(np.random.randn(len(date), 4), columns=list('BCDE'))], axis=1) df['date'] = date df.set_index("date", inplace = True) boo = [True, False] bool_matrix = pd.DataFrame(np.random.choice(boo, (len(date), 5),p=[0.3,.7]), index = date,columns=list('ABCDE')) def highlight(value): return 'background-color: green' my_style = df.style for column in df.columns: for i in bool_matrix.index: data = bool_matrix.loc[i, column] if data: my_style = df.style.use(my_style.export()).applymap(highlight, subset = pd.IndexSlice[i,column]) my_style Documentation The docs make reference to CSS Classes and say that "Index label cells include level where k is the level in a MultiIndex." I am obviouly indexing this wrong, but am stumped on how to proceed.
It's very nice that there is a runable example. You can use df.style.apply(..., axis=None) to apply a highlight method to the whole dataframe. With your df and bool_matrix, try this: def highlight(value): d = value.copy() for c in d.columns: for r in df.index: if bool_matrix.loc[r, c]: d.loc[r, c] = 'background-color: green' else: d.loc[r, c] = '' return d df.style.apply(highlight, axis=None) Or to make codes simple, you can try: def highlight(value): return bool_matrix.applymap(lambda x: 'background-color: green' if x else '') df.style.apply(highlight, axis=None) Hope this is what you need.
How can I count a specific value in group_by in pandas?
I have a dataframe and I use groupby to group it by Season. One of the columns of the original df is named Check and consists of True and False. My aim it to count the True values for each group and put it in the new dataframe. import pandas as pd df = .... df['Check'] = df['Actual'] == df['Prediction'] grouped_per_year = df.groupby('Season') df_2= pd.DataFrame() df_2['Seasons'] = total_matches_per_year.keys() df_2['Successes'] = '' df_2['Total_Matches'] = list(grouped_per_year.size()) df_2['SR'] = df_2['Successes'] / df_2['Total_Matches'] df_2['Money_In'] = list(grouped_per_year['Money_In'].apply(sum)) df_2['Profit (%)'] = (df_profit['Money_In'] - df_profit['Total_Matches']) / df_profit['Total_Matches'] * 100. I have tried: successes_per_year = grouped_per_year['Pred_Check'].value_counts() but I don't know how to get only the True count.
For counting True, you can also use sum (as True=1 and False=0 when doing a numerical operation): grouped_per_year['Pred_Check'].sum()