如何在 python pandas 列中添加 1,例如 row(n)=row(n-1)+1?
How can I add 1s in a python pandas column like row(n)=row(n-1)+1?
我需要创建一个简单的 python-pandas 列,如下所示:
(1607674395.805080)
(1607674396.805080)
(1607674397.805080)
(1607674398.805080)
(1607674399.805080)
第一行是 (time.time()) (自纪元以来的时间),我想像 row(n)=row(n-1)+1s 一样添加 1s 直到结束例如,下一列名为 'data'。
你可以使用shift方法:
import pandas as pd
import numpy as np
import time
pd.set_option('display.float_format', lambda x: '%.3f' % x)
df = pd.DataFrame(dict(a=np.random.random(5)))
df['new'] = np.arange(df.shape[0]) + time.time() # <<< this is the line that does the magic
df['strs'] = '(' + df['new'].round(6).astype(str) + ')'
>>> df
a new strs
0 0.119 1607948475.922 (1607948475.922189)
1 0.716 1607948476.922 (1607948476.922189)
2 0.561 1607948477.922 (1607948477.922189)
3 0.188 1607948478.922 (1607948478.922189)
4 0.995 1607948479.922 (1607948479.922189)
我需要创建一个简单的 python-pandas 列,如下所示:
(1607674395.805080)
(1607674396.805080)
(1607674397.805080)
(1607674398.805080)
(1607674399.805080)
第一行是 (time.time()) (自纪元以来的时间),我想像 row(n)=row(n-1)+1s 一样添加 1s 直到结束例如,下一列名为 'data'。
你可以使用shift方法:
import pandas as pd
import numpy as np
import time
pd.set_option('display.float_format', lambda x: '%.3f' % x)
df = pd.DataFrame(dict(a=np.random.random(5)))
df['new'] = np.arange(df.shape[0]) + time.time() # <<< this is the line that does the magic
df['strs'] = '(' + df['new'].round(6).astype(str) + ')'
>>> df
a new strs
0 0.119 1607948475.922 (1607948475.922189)
1 0.716 1607948476.922 (1607948476.922189)
2 0.561 1607948477.922 (1607948477.922189)
3 0.188 1607948478.922 (1607948478.922189)
4 0.995 1607948479.922 (1607948479.922189)