繁体   English   中英

将dataframe列中的值附加到字符串

[英]Append values in dataframe column to string

假设我有一个数据框

    col1        col2
0   1       John Constantine
1   2       ML Engineer
2   3       Colorado

我想将col2中的所有值附加到列表中,并且每一行应成为列表中的字符串值。

output = ['John Constantine', 'ML Engineer' , 'Colorado']

问题是我何时使用

output = df.col2 

输出再次变为数据帧。 即使我使用tolist()tolist()获得列表列表

这应该做

list(df.col2)

tolist仍然有效

df.col2.tolist()
Out[401]: ['JohnConstantine', 'MLEngineer', 'Colorado']

tolist()应该工作:

In [14]: df = pandas.DataFrame({
    "col1": [1, 2, 3], 
    "col2": ["John Constantine", "ML Engineer", "Colorado"]
})

In [15]: df.col2.tolist()
Out[15]: ['John Constantine', 'ML Engineer', 'Colorado']

我觉得你需要

df["col2"].tolist()

暂无
暂无

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

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