Home > Software design >  Pandas =right() on excel
Pandas =right() on excel

Time:11-03

On pandas 1.3.4 and Python 3.9.

So I'm trying to basically do a =RIGHT() function for an entire column of the column next to it. I am currently referencing this but I'm getting an error of Can only use .str accessor with string values!

This is my code:

import pandas as pd
df = pd.read_table('file.csv', delimiter=',')

df = pd.DataFrame(df, columns=['Cost', 'Caller'])

df['Cost'] = df['Caller'].str[-10:]

df.to_csv('file.csv') 

This is what I have, where the cost column is empty so it would be the =right() for the caller column just like this. The csv files that I'll be dealing will each have a different amount of "callers".

CodePudding user response:

Don't let Pandas infer your data type else Caller will cast as an integer.
Use dtype=str as parameter of read_table (or read_csv?)

df = pd.read_table('file.csv', delimiter=',', dtype=str)
  • Related