【问题标题】:Convert list of np.int64 to list of strings将 np.int64 列表转换为字符串列表
【发布时间】:2021-05-15 23:04:18
【问题描述】:

我正在尝试将 np.int64 值列表转换为熊猫系列中的字符串值。

列值如下所示:

1. [0000, 3321, 76463, 858505,...]
2. [00300, 2421, 769063, 853205,...]
3. [0000, 89321, 216463, 85899,...]
...

尝试使用循环进行迭代 -

for i in df['col']:
    for j in i:
        j = str(j)

并尝试了列表理解,但都没有奏效。 df['col'][i][j] 仍然是 np.int64。

理想情况下,我想将所有值“解包”为字符串序列,因此答案如下:

'0000' , '3321', '76463', '858505'...
'00300', '2421', '769063', '853205'...
...

【问题讨论】:

    标签: python pandas dataframe numpy


    【解决方案1】:

    策略:创建一个新的数据结构来填充旧数据结构中的值。这可能有帮助吗?

    stringData = [] #New empty structre for the string data.
    for i in range(len(df['col'])): #Iterate through each column
        stringData.append[] #Append a new column to the new structure
        for j in df['col'][i]: #Iterate through each item in each column
            stringData[i].append(str(j)) # Append a new item to the new column
    

    【讨论】:

    • stringData.append([])
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多