Converting data when reading csv in pandas
I am reading a CSV file into pandas framework. Csv file contains multiple columns. Column "A" contains the line "20 -989-98766". Is it possible to only read the last 5 characters "98766" from a string when loading a file?
df = pd.read_csv("test_data2.csv", column={'A':read the last 5 characters})
output:
A
98766
95476
.....
source to share
You can define func
and pass this as an arg parameter in converters
for read_csv
:
In [57]:
import io
import pandas as pd
def func(x):
return x[-5:]
t="""column
'20-989-98766"""
df = pd.read_csv(io.StringIO(t), converters={'column': func})
df
Out[57]:
column
0 98766
So here I am defining a func
and passing that in converters
in dict form with your column name as key, this will call func
on every line of your csv
so the following should work in your case:
df = pd.read_csv("test_data2.csv", converters={'A':func})
source to share