简体   繁体   中英

Fix the length of some columns using Pandas

I am trying to add some columns to a pandas dataFrame , but I cannot set the character length of the columns. I want to add the new fields as a string with a value of null and a length of two characters as the length of the field.

Any idea is welcome.

import pandas as pd
df[["Assess", "Operator","x", "y","z", "g"]]=None

If need fix length of columns in new DataFrame use:

from  itertools import product
import string

#length of one character
letters = string.ascii_letters
#print(len(letters)) #52

#if need length of two characters
#print(len(letters)) #2704
#letters = [''.join(x) for x in product(letters,letters)]

df = pd.DataFrame({'col1':[4,5], 'col':[8,2]})

#threshold
N = 5

#get new columns names by difference with original columns length
#min is used if possible negative number after subraction, then is set 0
cols = list(letters[:max(0, N- len(df.columns))])

#added new columns filled by None
#filter by threshold (if possible more columns in original like `N`)
df = df.assign(**dict.fromkeys(cols, None)).iloc[:, :N]
print (df)
   col1  col     a     b     c
0     4    8  None  None  None
1     5    2  None  None  None

Test if more columns like N threshold:

df = pd.DataFrame({'col1':[4,5], 'col2':[8,2],'col3':[4,5], 
                   'col4':[8,2], 'col5':[7,3],'col6':[9,0], 'col7':[5,1]})

print (df)
   col1  col2  col3  col4  col5  col6  col7
0     4     8     4     8     7     9     5
1     5     2     5     2     3     0     1


N = 5

cols = list(letters[:max(0, N - len(df.columns))])

df = df.assign(**dict.fromkeys(cols, None)).iloc[:, :N]
print (df)

   col1  col2  col3  col4  col5
0     4     8     4     8     7
1     5     2     5     2     3

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