Oct-20-2019, 03:17 PM
(This post was last modified: Oct-20-2019, 03:17 PM by edvvardbrian.)
Hello!
So I have a pandas dataframe filled with zeros, and I want to set the very first column and the very first row to the following values: ["a_1","a_2","a_3","b_4","b_5","b_6"].
I'm using this code:
For the life of me I can't figure out why this happens.
Thanks!
So I have a pandas dataframe filled with zeros, and I want to set the very first column and the very first row to the following values: ["a_1","a_2","a_3","b_4","b_5","b_6"].
I'm using this code:
import numpy as np import pandas as pd data_array = np.zeros([7, 7]) data_df = pd.DataFrame(data_array, index=["indexes","a","a","a","b","b","b"], columns=["columns","a","a","a","b","b","b"]) print(data_df, "\n") data_df.iloc[:,0] = [0] + ["a_1","a_2","a_3","b_4","b_5","b_6"] print(data_df, "\n") data_df.iloc[0,:] = [0] + ["a_1","a_2","a_3","b_4","b_5","b_6"] print(data_df, "\n")However, this does not work, because the final output is:
Output: columns a a a b b b
indexes 0.0 0.0 0.0 0.0 0.0 0.0 0.0
a 0.0 0.0 0.0 0.0 0.0 0.0 0.0
a 0.0 0.0 0.0 0.0 0.0 0.0 0.0
a 0.0 0.0 0.0 0.0 0.0 0.0 0.0
b 0.0 0.0 0.0 0.0 0.0 0.0 0.0
b 0.0 0.0 0.0 0.0 0.0 0.0 0.0
b 0.0 0.0 0.0 0.0 0.0 0.0 0.0
columns a a a b b b
indexes 0 0.0 0.0 0.0 0.0 0.0 0.0
a a_1 0.0 0.0 0.0 0.0 0.0 0.0
a a_2 0.0 0.0 0.0 0.0 0.0 0.0
a a_3 0.0 0.0 0.0 0.0 0.0 0.0
b b_4 0.0 0.0 0.0 0.0 0.0 0.0
b b_5 0.0 0.0 0.0 0.0 0.0 0.0
b b_6 0.0 0.0 0.0 0.0 0.0 0.0
columns a a a b b b
indexes 0 a_3 a_3 a_3 b_6 b_6 b_6
a a_1 0 0 0 0 0 0
a a_2 0 0 0 0 0 0
a a_3 0 0 0 0 0 0
b b_4 0 0 0 0 0 0
b b_5 0 0 0 0 0 0
b b_6 0 0 0 0 0 0
As you can see in the last table, the first column is set to the right values (a_1 a_2 a_3 b_4 b_5 b_6), but the first row is set instead to (a_3 a_3 a_3 b_6 b_6 b_6).For the life of me I can't figure out why this happens.
Thanks!