如果没有最小的工作示例,很难为您提供明确的建议,但我认为您正在寻找的是 .loc 一个 pd.DataFrame。我建议您执行以下操作:
- 如果首先将日期转换为日期时间,则使用
.loc 选择行在您的情况下效果更好,因此第一步是将此转换为:
# Pandas is quite smart about guessing date format. If this fails, please check the
# documentation https://docs.python.org/3/library/datetime.html to learn more about
# format strings.
df['date'] = pd.to_datetime(df['date'])
# Make this the index of your data frame.
df.set_index('date', inplace=True)
- 尚不清楚您打算如何使用条件/其他列的内容是什么。使用
.loc 这非常简单
# At Feb 1, 2020, add a value to columns 'var'.
df.loc['2020-02-01', 'var'] = 0.727868
# Assuming you have a second `df2` which as a datetime columns 'date' with the
# data you wish to add to `df`. This will only work if all df2['date'] are found
# in df.index. You can workout the logic for your case.
df.loc[df2['date'], 'var2'] = df2['vals']
如果逻辑过于复杂且数据框不太大,使用.iterrows 进行迭代可能会更容易,特别是如果您从 Python 开始。
for idx, row in df.iterrows():
if idx in list_of_other_dates:
df.loc[i, 'var'] = (some code here)
请澄清一下你的问题,你会得到更好的答案。不要忘记查看documentation。