How to append rows in pandas ,if column value is not in a external list
Question:
I have a DataFrame you can have it by running this code:
df = """
Facility Quarter Price
F1 Q1 2.1
F1 Q2 1.2
"""
df= pd.read_csv(StringIO(df.strip()), sep='ss+', engine='python')
And a list:
quarters=['Q1','Q2','Q3','Q4']
The logic is if the Quarter value is not in quarters list ,then append rows that assign the value of price to 0.
The output should looks like:
Facility Quarter Price
F1 Q1 2.1
F1 Q2 1.2
F1 Q3 0
F1 Q4 0
Answers:
You can use pd.MultiIndex.from_product
to recreate all combinations:
quarters=['Q1', 'Q2', 'Q3', 'Q4']
cols = ['Facility', 'Quarter']
mi = pd.MultiIndex.from_product([df['Facility'].unique(), quarters], names=cols)
out = df.set_index(cols).reindex(mi, fill_value=0).reset_index()
print(out)
# Output
Facility Quarter Price
0 F1 Q1 2.1
1 F1 Q2 1.2
2 F1 Q3 0.0
3 F1 Q4 0.0
Another way with pivot
:
>>> (df.pivot(index='Facility', columns='Quarter', values='Price')
.reindex(quarters, axis=1, fill_value=0)
.stack().rename('Price').reset_index())
Facility Quarter Price
0 F1 Q1 2.1
1 F1 Q2 1.2
2 F1 Q3 0.0
3 F1 Q4 0.0
this is my solution to your problem. I hope this helps.
import pandas as pd
data = {'Facility': ['F1', 'F1'], 'Quarter': ['Q1', 'Q2'], 'Price': [2.1, 1.2]}
df = pd.DataFrame(data)
quarters=['Q1','Q2','Q3','Q4']
#make dataframe bigger if needed
df = df.reindex(range(max(len(df), len(quarters))))
#turn list into pd.Series
se_quarters = pd.Series(quarters)
#repalce and add values to Quarter column
df['Quarter'] = se_quarters.values
#have everywhere the same value for column Facility
df['Facility'] = 'F1'
#replace the NaN values with 0
df = df.fillna(0)
Result:
I have a DataFrame you can have it by running this code:
df = """
Facility Quarter Price
F1 Q1 2.1
F1 Q2 1.2
"""
df= pd.read_csv(StringIO(df.strip()), sep='ss+', engine='python')
And a list:
quarters=['Q1','Q2','Q3','Q4']
The logic is if the Quarter value is not in quarters list ,then append rows that assign the value of price to 0.
The output should looks like:
Facility Quarter Price
F1 Q1 2.1
F1 Q2 1.2
F1 Q3 0
F1 Q4 0
You can use pd.MultiIndex.from_product
to recreate all combinations:
quarters=['Q1', 'Q2', 'Q3', 'Q4']
cols = ['Facility', 'Quarter']
mi = pd.MultiIndex.from_product([df['Facility'].unique(), quarters], names=cols)
out = df.set_index(cols).reindex(mi, fill_value=0).reset_index()
print(out)
# Output
Facility Quarter Price
0 F1 Q1 2.1
1 F1 Q2 1.2
2 F1 Q3 0.0
3 F1 Q4 0.0
Another way with pivot
:
>>> (df.pivot(index='Facility', columns='Quarter', values='Price')
.reindex(quarters, axis=1, fill_value=0)
.stack().rename('Price').reset_index())
Facility Quarter Price
0 F1 Q1 2.1
1 F1 Q2 1.2
2 F1 Q3 0.0
3 F1 Q4 0.0
this is my solution to your problem. I hope this helps.
import pandas as pd
data = {'Facility': ['F1', 'F1'], 'Quarter': ['Q1', 'Q2'], 'Price': [2.1, 1.2]}
df = pd.DataFrame(data)
quarters=['Q1','Q2','Q3','Q4']
#make dataframe bigger if needed
df = df.reindex(range(max(len(df), len(quarters))))
#turn list into pd.Series
se_quarters = pd.Series(quarters)
#repalce and add values to Quarter column
df['Quarter'] = se_quarters.values
#have everywhere the same value for column Facility
df['Facility'] = 'F1'
#replace the NaN values with 0
df = df.fillna(0)
Result: