Index error with pandas series

Hi everyone, I’m working with some networks and I need to build an algorithm for community detection.
I firstly randomized a solution with

random_solution = np.random.choice([1, 2], nodes_num)

nodes_num represents the number of nodes of the community.
Then I tried to create a dataframe with a random assignation of the links to the 2 communities with

communities = pd.concat([pd.Series(random_solution[net.iloc[:,0]]), pd.Series(random_solution[net.iloc[:,1]])], axis=1)

But it raises this error

IndexError: index 34 is out of bounds for axis 0 with size 34

What is strange it’s that if I just try to do


it works just fine and I can’t understand what is happening.

The network in question has 34 node and 78 links or edges.
Does someone know how I could solve this?

It’s been like all the day now…
Thank you.

I can’t test this right now because my python IDE is upgrading, but I think you mean axis=0 for concatenation because you are trying to concatenate something of size 34 to something of size 78, which presumably means you want to concatenate them one after the other rather than as two columns. When you do that, you’ll get a size 34+78 series, since you are concatinating a series to a series along the length. The reason you get the index error concatenating them as columns along axis 1 is because the first argument doesn’t have an index 34 when it continues along the second argument for column concatenation. If you do intend to concatenate columns with missing data for the 34 length column, you may first need to concatenate a series with zeroes to the end of the 34 length series to make them the same length.


Thank you! I solved by using lambda function and getting x from the random solution.