1π
β
It seems you need iloc
with assign to columns names:
df.columns = [df.iloc[0], df.iloc[1]]
and if need remove this rows:
df = df.iloc[2:].reset_index(drop=True)
df.columns.names = (None, None)
Sample:
df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9]})
print (df)
A B C
0 1 4 7
1 2 5 8
2 3 6 9
df.columns = [df.iloc[0], df.iloc[1]]
df = df.iloc[2:].reset_index(drop=True)
df.columns.names = (None, None)
print (df)
1 4 7
2 5 8
0 3 6 9
π€jezrael
1π
I canβt fully understand what you want to achieve, but I will try to guess:
pvm.columns = ['0', '1', *df.columns[2:]]
is it what you intend to get?
Example:
import pandas as pd
df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9]})
print (df)
print('==========')
df.columns = ['0', '1', *df.columns[2:]]
print (df)
gives:
"""
A B C
0 1 4 7
1 2 5 8
2 3 6 9
=========
0 1 C
0 1 4 7
1 2 5 8
2 3 6 9
"""
π€Claudio
- [Answered ]-Django Split a charfield output which is seperated by ','(comma) into an array of strings.?
- [Answered ]-Django: what is the difference between redirect to a view function and redirect to a url (from urls.py file)
- [Answered ]-Django makemigrations error when there are 3 custom model in a models.py
Source:stackexchange.com