python - 如何重写此代码以使其更易于阅读?

标签 python pandas

start=2014
df = pd.DataFrame({'age':past_cars_sold,}, index = [start, start+1,start+2,start+3,start+4,start+5,start+6])

有没有更简单的方法来重写这段代码。现在我一次做一个,只想知道是否有更简单的方法来重写它。

最佳答案

Karl's comment似乎是最直接的。不需要列表——只需给 Pandas 一个 range目的:

start = 2014
df = pd.DataFrame({'age': past_cars_sold}, index=range(start, start+7))

关于python - 如何重写此代码以使其更易于阅读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66810852/

相关文章:

python - 获取 DF 中最大值的索引名称

python - xlrd python库

python - 为什么在函数中使用字符串解包后,输出中会出现逗号?

python - 查找 ValueError : need more than X values to unpack 的缺失值

python - np.where 和 if 在 Pandas 中的组合

python-3.x - 如何修复 "OSError: [WinError 193] %1 is not a valid Win32 application"

python - 向 DataFrame 添加多行的有效方法

python - 在 Python3 中对具有 None 值的元组列表进行排序

python - 如何在 Pandas 数据框中将日期转换为 ISO-8601 DateTime 格式

python - Pandas/Python 过滤 DF 的列值