简体   繁体   中英

How to increment the Column and row names in Python Dataframe?

I have some set of data and inserting those data to the dataframe. But I don't know about number of rows and columns. How to increment the number of rows and columns name according to requirement.

Below I mentioned simple code.

cursor.execute("select * from incremental_paid")
                IncrementalPaidTriangleres = cursor.fetchall() 
                IncrementalPaidTriangledf = pd.DataFrame(IncrementalPaidTriangleres, columns=['DP1','DP2','DP3','DP4','DP5','DP6','DP7','DP8','DP9','DP10'], index=['OP1','OP2','OP3','OP4','OP5','OP6','OP7','OP8','OP9','OP10'])
                print(IncrementalPaidTriangledf)

Here I importing data from database and insert to the Dataframe which i created. According to the data dataframe have to increment the rows and columns name. Here dataframe column name starts with DP1 and row name with OP1. Need to increment the with DP1, DP2 so on till the data present. according to this rows also get increment. In above code rows and columns just fixed till DP10 and OP10.I want to auto increment.

You can just read the dataframe and change columns/index at once later

cursor.execute("select * from incremental_paid")
IncrementalPaidTriangleres = cursor.fetchall() 
IncrementalPaidTriangledf = pd.DataFrame(IncrementalPaidTriangleres)

numRows = IncrementalPaidTriangledf.shape[0]
numCols = IncrementalPaidTriangledf.shape[1]
columns = ["DP"+str(x) for x in range(1,numCols+1)]
index = ["OP"+str(x) for x in range(1,numRows+1)]

IncrementalPaidTriangledf.columns = columns
IncrementalPaidTriangledf.index = index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM