问题
I'm reading a .csv file into a pandas dataframe. The .csv file contains several columns. Column 'A' contains a string '20-989-98766'. Is it possible to only read the last 5 characters '98766' from the string when loading the file?
df = pd.read_csv("test_data2.csv", column={'A':read the last 5 characters})
output:
A
98766
95476
.....
回答1:
You can define a func
and pass this as an arg to converters
param 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 define a func
and pass this to converters
in the form of a dict with your column name as the key, this will call the func
on every row in your csv
so in your case the following should work:
df = pd.read_csv("test_data2.csv", converters={'A':func})
来源:https://stackoverflow.com/questions/43350601/convert-data-on-reading-csv-in-pandas