Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

python - Python:如何在Pandas列中缩短整数?(Python: how to shorten integers in a pandas column?)

I have a pandas dataframe like the following

(我有一个像下面的熊猫数据框)

df 
        T
0  1573119508850
1  1573119511513
2  1573119516821
3  1573119520387

I would like to shorten the number and approximate them with an approximation like the following:

(我想缩短数字,并用类似以下的近似值:)

df 
        T
0  1573119509
1  1573119512
2  1573119517
3  1573119520
  ask by emax translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you need divide by 1000 , round and last convert to integers:

(我认为您需要除以1000 ,四舍五入并最后转换为整数:)

df['T'] = df['T'].div(1000).round().astype(int)
print (df)
            T
0  1573119509
1  1573119512
2  1573119517
3  1573119520

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...