在 Pandas 中转换读取 csv 的数据 [英] Convert data on reading csv in pandas
问题描述
我正在将 .csv 文件读入 Pandas 数据帧..csv 文件包含多列.列A"包含字符串20-989-98766".加载文件时是否可以只从字符串中读取最后 5 个字符 '98766'?
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})
输出:
A
98766
95476
.....
推荐答案
您可以定义一个 func
并将其作为参数传递给 converters
param for read_csv
::>
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
所以在这里我定义了一个 func
并将它以 dict 的形式传递给 converters
,以您的列名作为键,这将调用 func
在您的 csv 中的每一行
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})
这篇关于在 Pandas 中转换读取 csv 的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!