ShanZhengYang
ShanZhengYang

Reputation: 17651

How to shift down columns row and re-assign column values in Pandas Dataframe?

I have the following pandas DataFrame:

import pandas as pd
import numpy as np
data = 'filename.dat'
df = pd.read_table(data)
print(df)

   0.098722  08l23252 243434214  5         True
0  0.469112 -0.282863 -1.509059  2         True
1  0.283421  1.224234  7.823421  2         False
2 -1.135632  1.212112 -0.173215  4         False
3      2.34     0.271    0.0932  4         True
4  0.119209 -1.044236 -0.861849  4         True
5 -2.104569 -0.494929  1.071804  4         False
6  0.469112 -0.282863 -1.509059  3         True
7  1.023236  1.224234  7.823421  3         False
8 -1.135632  1.212112 -0.173215  3         False
9  23.23422  2.613492  2.341592  1         True
....

The data in filename.dat is saved such the the first row is considered to be the columns.

df.columns

outputs

Index(['0.098722', '08l23252', '243434214', '5', 'True'], dtype='object')

How do I add a row such that the current "columns" values row shifts down into the data table, and I could rename columns by actual column names?

At the moment, I cannot try

df.columns = ['A1', 'B1', 'C1', 'D1', 'E1']

as this simply erases this row and overwrites the values with A1, B1, etc.

   A1        B1        C1        D1        E1
1  0.283421  1.224234  7.823421  2         False
2 -1.135632  1.212112 -0.173215  4         False
3      2.34     0.271    0.0932  4         True
4  0.119209 -1.044236 -0.861849  4         True
....

Upvotes: 0

Views: 1360

Answers (2)

jezrael
jezrael

Reputation: 863761

Add parameter names:

df = pd.read_table(datan, names=['A1', 'B1', 'C1', 'D1', 'E1'])

But it looks like better id use read_csv:

import pandas as pd
import io

temp=u"""
9,40  
1,70"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), names=['b','m'])
print (df)
   b   m
0  9  40
1  1  70

Upvotes: 2

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210982

try this:

df = pd.read_table(data, names=['A1', 'B1', 'C1', 'D1', 'E1'], header=None)

from docs:

names : array-like, default None

List of column names to use. If file contains no header row, then you should explicitly pass header=None

Upvotes: 3

Related Questions