Convert data on reading csv in pandas

那年仲夏 提交于 2021-02-16 13:17:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!