简体   繁体   English

从熊猫数据框中的字符串数组中获取第一个数值

[英]Take first numeric value from string array in pandas dataframe

I have columns in my pandas dataframe that come in the following format for example:例如,我的 Pandas 数据框中的列采用以下格式:

df['X']:

0      [0.8242424242424241, 1.511111111111111, 2.9191...
1      [1.236363636363636, 2.438383838383838, 3.09090...
2                [1.064646464646464, 2.5757575757575752]
3      [0.583838383838383, 1.373737373737373, 2.02626...
4      [0.7898989898989891, 1.751515151515151, 2.6444...
                             ...                        
135    [1.236363636363636, 1.751515151515151, 2.26666...
136    [1.202020202020202, 2.1292929292929292, 2.7818...
137    [0.583838383838383, 1.476767676767676, 3.15959...
138    [1.236363636363636, 2.61010101010101, 3.090909...
139    [1.339393939393939, 2.7818181818181813, 3.1252...
Name: X, Length: 140, dtype: object

where df['X'][0] for example is a fully stringed array as follows:例如,其中df['X'][0]是一个完整的字符串数组,如下所示:

'[0.8242424242424241, 1.511111111111111, 2.919191919191919]'

Essentially each entry is a array/vector coming through and is, as a whole, a string (note that it's NOT just the individual numeric values that are strings but the array as a whole)本质上,每个条目都是一个数组/向量,并且作为一个整体,是一个字符串(请注意,不仅仅是单个数值是字符串,而是整个数组)

I want to be able to take just the first numeric value in the string vector/array and place that in the cell of the pandas column (in place of the string array) - how can I do this?我希望能够只取字符串向量/数组中的第一个数值并将其放在 pandas 列的单元格中(代替字符串数组) - 我该怎么做?

import ast
df['a'].apply(ast.literal_eval).str[0]

To convert each string representation of a list ( str_lst ) to a list you can use ast.literal_eval .要将列表 ( str_lst ) 的每个字符串表示形式转换为列表,您可以使用ast.literal_eval Then you just need to index the first element, ie ast.literal_eval(str_lst)[0] .然后你只需要索引第一个元素,即ast.literal_eval(str_lst)[0]

import ast 

df['X'] = df['X'].map(lambda str_lst: ast.literal_eval(str_lst)[0])

使用pd.eval

df['X'] = pd.eval(df['X'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Python Pandas 中的 DataFrame 中的列中获取字符串值? - How to take part of string value from column in DataFrame in Python Pandas? 将 Pandas DataFrame 列值从数组更改为数组中的第一个值 - Change Pandas DataFrame column values from array to first value in array 如何根据值的第一个字符获取 pandas dataframe 的子集? - How to take a subset of a pandas dataframe, based on the first characters of a value? 替换字符串中的第一个值 dataframe pandas - replace the first value in a string dataframe pandas Python pandas dataframe:在数组列中,如果第一项包含特定字符串,则从数组中删除该项 - Python pandas dataframe : In an array column, if first item contains specific string then remove that item from array 计算 pandas dataframe 中每一列的第一个值的增长率并返回 Numpy 数组 - Calculate the growth ratio from first value in every column in a pandas dataframe and return a Numpy array Pandas 用来自其他 dataframe 的字符串替换值 - Pandas replacing value with string from other dataframe 在 Numpy 数组中的 Pandas Dataframe 中添加值 - Add value in Pandas Dataframe from Numpy Array 使pandas DataFrame中的每个数值都为负数 - Make every numeric value in a pandas DataFrame negative 检查 pandas dataframe 中的列值是否为数字 - Check if a column value is numeric in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM