【问题标题】:Converting a string obtained from a Pandas dataframe into individual lists per line将从 Pandas 数据帧获得的字符串转换为每行单独的列表
【发布时间】:2019-03-15 02:33:48
【问题描述】:

test 是一个转换为字符串的 pandas 数据框。

strtest = (test.to_string())
print strtest

转换为字符串后,我有以下输出:

This is the first test file     98128612.12
This is the second test file    31236164.15

我正在尝试将字符串的每一行放入一个列表并打印出来,如下所示:

['This is the first test file','98128612.12']
['This is the second test file','31236164.15']

这是我尝试在列表中生成上述输出时的代码:

testlist = []

for row in strtest.iterrows():
        index, data = row
        testlist.append(data.tolist())

print testlist

但是当我运行它时,我遇到了这个错误我该如何解决这个问题:

     for row in strtest.iterrows():
 AttributeError: 'unicode' object has no attribute 'iterrows'

【问题讨论】:

    标签: python-2.7 pandas list for-loop itertools


    【解决方案1】:

    我认为你需要:

    testlist = test.values.tolist()
    print (testlist)
    [['This is the first test file', 98128612.12],
     ['This is the second test file', 31236164.15]]
    

    您的代码可以使用,but not recommended,因为速度慢:

    testlist = []
    #change strtest to test DataFrame
    for index, data in test.iterrows():
            testlist.append(data.tolist())
    
    print (testlist)
    

    【讨论】:

    • 我之前尝试过 testlist = test.values.tolist() 但是,我只得到浮点值。 “这是第一个测试文件”字符串根本没有打印出来。输出如下:[98128612.12,31236164.15]
    • @SamT - 所以需要test.astype(str).values.tolist()
    • 感谢您的帮助,但它似乎不起作用。您的第二个解决方案现在生成:AttributeError: 'Series' object has no attribute 'iterrows'
    • @SamT - 这是系列,你需要for index, data in test.reset_index().iterrows():
    • @SamT - 第一个解决方案testlist = test.reset_index().values.tolist()
    猜你喜欢
    • 2018-03-31
    • 2023-01-21
    • 2014-06-07
    • 2015-04-03
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多