[英]Convert data on reading csv in pandas
I'm reading a .csv file into a pandas dataframe. 我正在将.csv文件读入pandas数据帧。 The .csv file contains several columns.
.csv文件包含多个列。 Column 'A' contains a string '20-989-98766'.
列'A'包含字符串'20 -989-98766'。 Is it possible to only read the last 5 characters '98766' from the string when loading the file?
是否可以在加载文件时从字符串中读取最后5个字符'98766'?
df = pd.read_csv("test_data2.csv", column={'A':read the last 5 characters})
output: 输出:
A
98766
95476
.....
You can define a func
and pass this as an arg to converters
param for read_csv
: 您可以定义一个
func
并将其作为arg传递给read_csv
converters
参数:
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 所以在这里我定义一个
func
并以dict的形式将其传递给converters
,并以列名作为键,这将在csv中的每一行调用func
so in your case the following should work: 所以在你的情况下,以下应该工作:
df = pd.read_csv("test_data2.csv", converters={'A':func})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.