Mar-21-2024, 01:54 AM
wtd = pd.DataFrame(columns = df.columns) indexarr = [] appender = 0 for index in range(1, df.shape[0]): if df.iat[index, 1] == df.iat[index-1, 1]: indexarr.append(index-1) appender = 1 wtd = wtd._append(df.iloc[index-1], ignore_index=True) elif appender: indexarr.append(index-1) wtd = wtd._append(df.iloc[index-1], ignore_index=True) appender = 0 print(indexarr) print(wtd) del indexarr del wtd indexarr = [] wtd = pd.DataFrame(columns = df.columns)So full disclosure - I'm a python newbie here. This is my first time working with python. In a nutsell, df is a pandas dataframe that I've read in from an excel spreadsheet. wtd is an empty dataframe with the same columns as df. So what my for loop is doing is just parsing the df dataframe, and in a particular column if there are duplicate values in subsequent rows, I want to identify those duplicate subsequent row and create a new dataframe wtd that is composed of those subsequent rows. indexarr is an array that I just put in there to help me keep track of things. For example:
Output:[6, 7, 8]
[9, 10, 11, 12, 13]
[15, 16]
[19, 20]
[22, 23]
[24, 25]
[29, 30, 31, 32]
[33, 34]
Each one of those is an array of the indices of rows that have the same subsequent field in a specified column. So I want to copy the ENTIRE row from dataframe df and insert it into a new dataframe wtd - the indices on wtd can be 0,1,2 etc that's not important, and dataframe wtd gets deleted and remade as blank after each "set". What I'm intending to do is pass wtd to a function and get a return value, but I don't have that part coded in yet. My code works, but I get the warning
Error:FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.
wtd = wtd._append(df.iloc[index-1], ignore_index=True)
I have absolutely no idea how to fix this. I would like to write proper code and have this fixed. So what do I do? Thanks.