'How to add stuff by index

Seems like Polars is not a fan of Pandas indexes.

There are some things that you can do with indexes that I don't know how to do in Polars.

For example say I have test = pd.DataFrame({'a':['a','b'],'b':[3,4]}) and test1 = pd.DataFrame({'a':['a','b'],'b':[4,5]}), in Pandas I can add the two (with a being the index column).

In Polars when I try to add the two, I get string concatenation behavior for the a column, which I don't want. Is there a way to do what I want here?



Solution 1:[1]

As pointed out in the comments by ritchie46 you can use a join like this:

# pandas version with index and implicit join on add
import pandas as pd
test1 = pd.DataFrame({'a':['a','b'],'b':[3,4]}).set_index('a')
test2 = pd.DataFrame({'a':['a','b'],'b':[4,5]}).set_index('a')
result = test1 + test2
print(result)

?????????????  
? a   ? b   ?  
?????????????  
? a   ? 7   ?  
?????????????  
? b   ? 9   ?  
?????????????  
# polars version with explicit join
import polars as pl
test1 = pl.DataFrame({'a':['a','b'],'b':[3,4]})
test2 = pl.DataFrame({'a':['a','b'],'b':[4,5]})
result = test1.join(test2, on='a').select(['a', pl.col('b')+pl.col('b_right')])
print(result)

?????????????
? a   ? b   ?
? --- ? --- ?
? str ? i64 ?
?????????????
? a   ? 7   ?
?????????????
? b   ? 9   ?
?????????????

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 datenzauber.ai