简体   繁体   English

Python Pandas:将对象列表转换为整数列表

[英]Python Pandas: convert list of objects to a list of integer

Hi I have a prboblem to convert list of objects to a list of integers . 嗨,我有一个问题将对象列表转换为整数列表 The objects are within the "stopsequence" column of the Pandas data frame "Kanten". 这些对象位于熊猫数据框“ Kanten”的“ stopsequence”列中。 All of this I receive after so CSV importing and data cleaning in the column. 在CSV导入和列中的数据清理之后,我收到了所有这些信息。 I am using Python 3.X 我正在使用Python 3.X

I am a Python newbie, maybe that's part of the problem here. 我是Python新手,也许这是问题的一部分。

import pandas as pd
import numpy as np
import os
import re
import ast
orgn_csv = pd.read_csv(r"Placeholder path for csv file")
df = orgn_csv.dropna()
Kanten = pd.DataFrame({"stopsequence" : df.stopsequence})

# In between is a block in which I use regular expressions for data cleaning purposes.
# I left the data cleaning block out to make the post shorter


Kanten.stopsequence = Kanten.stopsequence.str.split (',')
print (Kanten.head())
print (Kanten.stopsequence.dtype)                      

This gives the following output: 这给出以下输出:

                                        stopsequence
2  [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
3  [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
4  [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
5  [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
6  [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
object

I am looking for a way to transform the list which contains objects. 我正在寻找一种方法来转换包含对象的列表。 I searched through the StackOverflow Forum intensively and tried a bunch of different approaches. 我仔细搜索了StackOverflow论坛,并尝试了许多不同的方法。 With none of them I was succesfull. 没有他们,我就成功了。 I tryed to use: 我尝试使用:

astype(str).astype(int) astype(str).astype(int)

Kanten.stopsequence = Kanten.stopsequence.astype(str).astype(int)
This Returns:
ValueError: invalid literal for int() with base 10:

adapted the following post with the use of atoi instead of atof 使用atoi而不是atof修改了以下帖子

Kanten.stopsequence.applymap(atoi)
This Returns:
AttributeError: 'Series' object has no attribute 'applymap'

list(map()) 列表(map())

Kanten.stopsequence = list(map(int, Kanten.stopsequence))
This returns:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

apply(ast.literal_eval) 申请(ast.literal_eval)

Kanten.stopsequence = Kanten.stopsequence.apply(ast.literal_eval)
This returns:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

Does anybody see a solution for that? 有人看到解决方案了吗? I am uncertain if it's a complicated case or I just lacke some further programming experience. 我不确定这是一个复杂的案例还是我缺乏进一步的编程经验。 If possible a short explanation would be helpful. 如果可能的话,简短的解释会有所帮助。 That I can find a solution myself againg. 我自己也可以找到解决办法。 Thank you in advance. 先感谢您。

A pandas Series can be trivially converted to a list, and a list of lists can be given as input to create a DataFrame . 可以将pandas Series轻松地转换为列表,并且可以将列表列表作为创建DataFrame输入。

I think this could help: 我认为这可以帮助:

splitted = pd.DataFrame(Kanten.stopsequence.str.split (','), index=Kanten.index).astype(int)

This gives you a new dataframe with same index as the original one but where each element is in its own column. 这将为您提供一个新的数据框,其索引与原始索引相同,但是每个元素都在其自己的列中。

If relevant, you could then concat that new columns 如果相关,则可以合并该新列

pd.concat([Kanten, splitted], axis=1)

So from your second attempt at manipulating the data, your error message tells you that Kanten.stopsequence is a Series , not a DataFrame . 因此,从您第二次尝试操作数据时,错误消息告诉您Kanten.stopsequenceSeries ,而不是DataFrame To convert, you'd need to access 要进行转换,您需要访问

list_of_lists = Kanten.stopsequence.to_numpy(dtype='int32').tolist()

Note that for your data this will create a nested 2d data array. 请注意,这将为您的数据创建一个嵌套的2d数据数组。 To access the first integer from the first row, you would need to write list_of_lists[0][0] . 要访问第一行中的第一个整数,您需要编写list_of_lists[0][0]

This is how I would approach pulling the last column of a DataFrame into a list of ints. 这就是我将DataFrame的最后一列拉入一个int列表的方法。

Let's say the .csv is located in the same directory as your .py script and it's called kanten.csv . 假设.csv.py脚本位于同一目录中,称为kanten.csv The column you're looking for is stopsequence . 您要查找的列是stopsequence

import os
import pandas as pd

path=os.getcwd()
filename = 'kanten.csv'
filepath = os.path.join(path, filename)

kanten = pd.read_csv(filepath)
list = list(kanten['stopsequence'].apply(lambda x: int(x)))

In the last line, the stopsequence column is pulled from kanten , the values are casted as integers, then the column is converted to a standard python list object. 在最后一行中,从stopsequence中提取了stopsequence列,将值kanten转换为整数,然后将该列转换为标准的python列表对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM