我有这一天到一年& &;月份转换,我将它们实现到pandas dataframe
import numpy as np
import math
import matplotlib.pyplot as plt
import pandas as pd
day_len = math.floor((np.random.randint(0,100))/2)
if day_len != 0:
day = [i for i in (np.random.randint(1,1000,day_len))]
else:
pass
data = pd.DataFrame({'Day': day})
data['Year'] = data['Day'].apply(lambda x: (x/365))
data['Month'] = data['Day'].apply(lambda r: math.floor((r*0.032855)))
data
输出:
Day Year Month
0 419 1.147945 13
1 453 1.241096 14
2 421 1.153425 13
3 448 1.227397 14
4 638 1.747945 20
5 13 0.035616 0
6 769 2.106849 25
7 367 1.005479 12
8 318 0.871233 10
对于Year列,我希望值显示左侧唯一的第一个整数和第一个小数点整数,我想要的输出示例:
Year
1.1
1.2
1.5
2.1
如何编写一个代码,构建从上面的例子我想要什么?
你可以试试
df['Year'] = df['Year'].round(1)
Day Year Month
0 419 1.1 13
1 453 1.2 14
2 421 1.2 13
3 448 1.2 14
4 638 1.7 20
5 13 0.0 0
6 769 2.1 25
7 367 1.0 12
8 318 0.9 10