簡體   English   中英

使用 Pandas 修復某些列的長度

[英]Fix the length of some columns using Pandas

我正在嘗試向pandas dataFrame 添加一些列,但我無法設置列的字符長度 我想將新字段添加為字符串,值為 null長度為兩個字符作為字段的長度。

歡迎任何想法。

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

如果需要修復新 DataFrame 中列的長度,請使用:

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

測試是否有更多列像N閾值:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM