Andrei Cozma
Andrei Cozma

Reputation: 1030

fill dataframe column equally

I have a dataframe that looks as follows:

ID    Name
1     Missing
2     Missing
3     Missing
.......

Is there a way in which I could fill in the Column Name equally(+1) if len(df) is uneven with a number of names I have stored( a list or a dictionary). For Ex if I have 2 names. Half of the column would be Name1 while the other half would be Name2. I tried:

for i in (range(len(df)/no_names)):
    counter=0
    df.ix[i]['Name'] = dictionary.values()[0]

but this would fill in only my first N rows based on how many names I have.

Upvotes: 1

Views: 821

Answers (2)

joel.wilson
joel.wilson

Reputation: 8413

my first try at python questions, This is definitely not the most efficient solution.

import pandas as pd
df = pd.DataFrame({'a':[1,4,4,0,4,0,4,0],'b':[2,1,4,0,4,0,4,0]})
#df
#Out[76]: 
#   a  b
#0  1  2
#1  4  1
#2  3  3
#3  4  4
#4  0  0
#5  4  4
#6  0  0
#7  4  4
#8  0  0

based on the length of each column, repeat Name1 and Name2 accordingly

df['new'] = np.repeat(np.array(["A", "B"]), repeats=[round(df.shape[0]/2), df.shape[0]-round(df.shape[0]/2)])

#Out[81]: 
#   a  b new
#0  1  2   A
#1  4  1   A
#2  3  3   A
#3  4  4   A
#4  0  0   B
#5  4  4   B
#6  0  0   B
#7  4  4   B
#8  0  0   B

Upvotes: 1

unutbu
unutbu

Reputation: 879511

You could use

import numpy as np
N = len(df)
df['Name'] = np.array(['Name1', 'Name2'])[np.linspace(0,2,N,endpoint=False).astype(int)]

The idea here is to create an array of 0's and 1's, such as

In [34]: np.linspace(0,2,11,endpoint=False).astype(int)
Out[34]: array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])

Now we can use NumPy indexing to create an array of 'Name1' and 'Name2' values:

In [8]: np.array(['Name1', 'Name2'])[np.linspace(0,2,11,endpoint=False).astype(int)]
Out[8]: 
array(['Name1', 'Name1', 'Name1', 'Name1', 'Name1', 'Name1', 'Name2',
       'Name2', 'Name2', 'Name2', 'Name2'], 
      dtype='<U5')

Upvotes: 2

Related Questions