Mutate pandas dataframe cell values

I want to mutate column c at a specific row by adding that row and another row. df = pd.DataFrame({ ‘A’: [0,1,2,3], ‘B’: [0,1,2,3], ‘C’: [10,10,10,10] }) mask1 = df[‘A’]==1 mask2 = df[‘B’]==2 df.loc[mask1, ‘C’] = df.loc[mask1, ‘C’] + df.loc[mask2, ‘C’] In the last line, because we are adding two pd.Series together, it tries to… Read More Mutate pandas dataframe cell values

Pandas – Remove part of string in column that is already in another column

I have this dataframe : dfA = pd.DataFrame({ ‘A’: [‘abc’,’ghi’,’mno’, ‘stu’], ‘B’: [‘abcdef’, ‘jklghi’, ‘mnopqr’, ‘vwxstu’] }) dfA And I want to get this dataframe : dfB = pd.DataFrame({ ‘A’: [‘abc’,’ghi’,’mno’, ‘stu’], ‘B’: [‘abcdef’, ‘jklghi’, ‘mnopqr’, ‘vwxstu’], ‘C’: [‘def’, ‘jkl’, ‘pqr’, ‘vwx’], }) dfB The column ‘C’ must contains the substrings of the column ‘B’… Read More Pandas – Remove part of string in column that is already in another column

How to save dataframe to dictionary of different length

So I have a data frame that looks like this 2018-01-01 00:00:00 False 2018-01-01 00:30:00 False 2018-01-01 01:00:00 False 2018-01-01 01:30:00 True 2018-01-01 02:00:00 True 2018-01-01 02:30:00 True 2018-01-01 03:00:00 False 2018-01-01 03:30:00 False 2018-01-01 04:00:00 True 2018-01-01 04:30:00 True and it would continue for a full year. I want to save each chunk that… Read More How to save dataframe to dictionary of different length

How to filter out multiple rows in a pandas.DataFrame based on multiple conditions for the same column

I have an exemplary pd.DataFrame containing codenames of software developed in different development studios: df = pd.DataFrame({‘project_id’: [36423, 28564, 96648, 96648, 10042, 68277, 68277, 68277], ‘codename’: [‘banana’, ‘apple’, ‘peach’, ‘peach’, ‘melon’, ‘pear’, ‘pear’, ‘pear’], ‘studio’: [‘paris’, ‘amsterdam’, ‘frankfurt’, ‘paris’, ‘london’, ‘brussel’, ‘amsterdam’, ‘sofia’]}) id codename studio 0 36423 banana paris 1 28564 apple amsterdam 2… Read More How to filter out multiple rows in a pandas.DataFrame based on multiple conditions for the same column

How can I save multiple dataframes onto one excel file (as separate sheets) without this error occurring?

I have the following Python code: import pandas as pd path=r"C:\Users\Wali\Example.xls" df1=pd.read_excel(path, sheet_name = [0]) df2=pd.read_excel(path, sheet_name = [1]) with pd.ExcelWriter(r"C:\Users\Wali\Example2.xls") as writer: # use to_excel function and specify the sheet_name and index # to store the dataframe in specified sheet df1.to_excel(writer, sheet_name="1", index=0) df2.to_excel(writer, sheet_name="2", index=1) I’m reading the excel file which contains two… Read More How can I save multiple dataframes onto one excel file (as separate sheets) without this error occurring?

Pandas : Create new column based on text value of another column

This might be very simple question, but here’s my dataframe: id text position labels 0 39088 skin melanoma [58.0, 71.0] indication 1 39088 proteinase [137.0, 147.0] protein 2 39088 plasminogen activator [170.0, 191.0] protein 3 39088 NaN [nan, nan] NaN 4 39088 NaN [nan, nan] NaN 5 39088 proteinase substrates [36.0, 57.0] protein 6 39088… Read More Pandas : Create new column based on text value of another column

Pandas : change values in column based on a mapping of two different columns

I have this dataframe: id result.value.text result.value.labels result.id result.from_id result.to_id 0 793 skin melanoma indication 5jSiC_n3IM NaN NaN 1 793 proteinase protein Lso-iCCHar NaN NaN 2 793 plasminogen activator protein _17D_kE5zf NaN NaN 3 793 NaN NaN NaN 5jSiC_n3IM Lso-iCCHar 4 793 NaN NaN NaN 5jSiC_n3IM _17D_kE5zf I want to change the values of result.from_id… Read More Pandas : change values in column based on a mapping of two different columns