我需要遍历数据帧以获取第一个日期的值以移动到下一行并以相同的第一个值开始。重要的是,我需要在列范围内停止新输入,而不是在下面的示例中超过第5天溢出。
当前输出:
day1 day2 day3 day4 day5
date
2018-03-16 1.0 2.0 3.0 4.0 5.0
2018-03-17 NaN NaN NaN NaN NaN
2018-03-18 NaN NaN NaN NaN NaN
2018-03-19 NaN NaN NaN NaN NaN
2018-03-20 NaN NaN NaN NaN NaN
期望的输出:
day1 day2 day3 day4 day5
date
2018-03-16 1.0 2.0 3.0 4.0 5.0
2018-03-17 NaN 1.0 2.0 3.0 4.0
2018-03-18 NaN NaN 1.0 2.0 3.0
2018-03-19 NaN NaN NaN 1.0 2.0
2018-03-20 NaN NaN NaN NaN 1.0
迭代示例代码:
data = [1, 2, 3, 4, 5]
columns_name = ['day1', 'day2', 'day3', 'day4', 'day5']
df = pd.DataFrame(data)
df = df.T
df.columns = columns_name
dates = pd.date_range('2018-03-16', '2018-03-20').tolist()
dates_df = pd.DataFrame(dates)
dates_df.columns = ['date']
dfs = [df, dates_df]
combined = pd.concat(dfs, axis=1)
combined = combined.set_index(['date'])
答案 0 :(得分:4)
v = df.values
i, j = np.triu_indices(v.shape[1])
v[i, j] = v[0][j - i]
df
day1 day2 day3 day4 day5
date
2018-03-16 1.0 2.0 3.0 4.0 5.0
2018-03-17 NaN 1.0 2.0 3.0 4.0
2018-03-18 NaN NaN 1.0 2.0 3.0
2018-03-19 NaN NaN NaN 1.0 2.0
2018-03-20 NaN NaN NaN NaN 1.0
使用三角形索引切割第一行并指定
df.values
如果发生这种情况不适合您,因为v = df.values
i, j = np.triu_indices(v.shape[1])
v[i, j] = v[0][j - i]
df.loc[:] = v
df
day1 day2 day3 day4 day5
date
2018-03-16 1.0 2.0 3.0 4.0 5.0
2018-03-17 NaN 1.0 2.0 3.0 4.0
2018-03-18 NaN NaN 1.0 2.0 3.0
2018-03-19 NaN NaN NaN 1.0 2.0
2018-03-20 NaN NaN NaN NaN 1.0
是副本而不是视图:
numpy.lib.stride_tricks.as_strided
from numpy.lib.stride_tricks import as_strided as strided
n = df.shape[1]
v = np.append([np.nan for _ in range(n - 1)], df.values[0])
s = v.strides[0]
df.loc[:] = strided(v[n - 1:], df.shape, (-s, s))
df
day1 day2 day3 day4 day5
date
2018-03-16 1.0 2.0 3.0 4.0 5.0
2018-03-17 NaN 1.0 2.0 3.0 4.0
2018-03-18 NaN NaN 1.0 2.0 3.0
2018-03-19 NaN NaN NaN 1.0 2.0
2018-03-20 NaN NaN NaN NaN 1.0
不推荐
但仍然很有趣
function textIcon(_holder, _icon, _text, _on_click) {
_holder.append(`
<text-icon onclick="${() => { _on_click() }}">
<icon class="${_icon}"></icon>
<text>${_text}</text>
</text-icon>
`);
}