Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add the missing numbers in the table in order

Tags:

python

pandas

I need modify a csv file with pandas. I have the following table:

Interface   Description
1           Used
2           Used
3           Used
4           Used
6           Used
8           Used
12          Used
17          Used

I need to match the "Interface" column with a range of 1, 20, complete the table with the missing numbers and place the word "free" in the "Description" column and order it like this:

Interface   Description
1           Used
2           Used
3           Used
4           Used
5           free
6           Used
7           free
8           Used
9           free
10          free
11          free
12          Used
13          free
14          free
15          free
16          free
17          Used
18          free
19          free
20          free
like image 807
Martin Barbieri Avatar asked Sep 02 '25 06:09

Martin Barbieri


1 Answers

Use merge in combination with fillna

df = pd.DataFrame({
    'Interface': [1, 2, 3, 4, 6, 8, 12, 17],
    'Description': 'Used'})
df2 = pd.DataFrame({'Interface': range(1, 21)}).merge(df, how="left").fillna("free")
like image 118
bitflip Avatar answered Sep 04 '25 20:09

bitflip