[英]IF Statement - Creating New Column Based on Two Columns
I am trying to create a new column that contains the past values of df2.past_values
and the future values of df2.future_value
called df2['past_future_mix']
.我想创建一个包含过去值的新列df2.past_values
和未来值df2.future_value
称为df2['past_future_mix']
If window = 0, these are past values, window = 1, these are future values如果 window = 0,这些是过去的值,window = 1,这些是未来的值
I am trying to use a if elif statement, however i get this error:我正在尝试使用 if elif 语句,但是我收到此错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
import pandas as pd
import numpy as np
from datetime import timedelta
# Set up input data (taken from original post)
df1 = pd.DataFrame({
'past_value': [100, 200, 200, 300, 350, 400, 'NaN', 'NaN', 'NaN', 'NaN', 'NaN', 'NaN'],
'future_value': ['NaN', 'NaN', 'NaN', 'NaN', 'NaN', 'NaN', 800, 900, 900, 950, 975, 1000],
'window': [0,0,0,0,0,0,1,1,1,1,1,1],
'category': ['Category A']*4 + ['Category B']*4 + ['Category C']*4})
ca_list = df1.category.unique()
bigdf = pd.DataFrame()
for cat in cat_list:
df2 = df1[(df1.category == cat)]
if df2['window'] == 0:
df2['past_future_mix'] = df2['past_value']
elif df2['window'] == 1:
df2['past_future_mix'] = df2['future_value']
bigdf = bigdf.append(df2)
This is the error message that i am getting:这是我收到的错误消息:
ValueError Traceback (most recent call last)
<ipython-input-56-cfdd200ce9ff> in <module>
16 for cat in cat_list:
17 df2 = df1[(df1.category == cat)]
---> 18 if df2['window'] == 0:
19 df2['past_future_mix'] = df2['past_value']
20 elif df2['window'] == 1:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1553 "The truth value of a {0} is ambiguous. "
1554 "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
-> 1555 self.__class__.__name__
1556 )
1557 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I changed if df2['window] == 0
to if 0 in df2['window']
, however it does not print a dataframe now.我将if df2['window] == 0
更改为if 0 in df2['window']
,但是它现在不打印数据帧。
for cat in cat_list:
df2 = df1[(df1.category == cat)]
if 0 in df2['window']:
df2['past_future_mix'] = df2['past_value']
elif 1 in df2['window']:
df2['past_future_mix'] = df2['future_value']
bigdf = bigdf.append(df2)
print(bigdf)
You can try .loc
你可以试试.loc
for cat in cat_list:
df2 = df1[(df1.category == cat)]
df2.loc[df2['window'] == 0, 'past_future_mix'] = df2['past_value']
df2.loc[df2['window'] == 1, 'past_future_mix'] = df2['future_value']
bigdf = bigdf.append(df2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.