[英]Convert object column to array type - pandas.DataFrame
Im reading a SQL query with pd.read_sql().我正在使用 pd.read_sql() 读取 SQL 查询。
One of the columns of the query has array type, but Pandas doesn't recognize this as an array, but as a string.查询的其中一列具有数组类型,但 Pandas 不将其识别为数组,而是将其识别为字符串。
How do I change de type of the column to be able to iterate over its values?如何更改列的 de 类型以能够迭代其值?
This is the df head:这是df头:
main_ID related_ids
1 00088381096959 [PRDKQUOV2JAR17RD, PRDKPYH11Z4GFZOV]
2 0011509002051 [PRCOFOK9MJ2CTABE, PRSESJD2K7PFYVUM]
3 0011509002051 [OSPAA89FNMJH9UWK, OSPW2JJQ949ZHNS3]
4 0019954960568 [PRDORZT50BDS1Z6H, PRDHG07MVG1YCX2N]
Then I will need to do something like this:然后我需要做这样的事情:
for item,row in df.iterrows():
for id in row['related_ids']:
code...
TRY:尝试:
via strip()
and split()
:通过
strip()
和split()
:
df['related_ids']=df['related_ids'].str.strip('[]').str.split(',')
OR或者
If you need np.array()
then use:如果你需要
np.array()
然后使用:
df['related_ids']=df['related_ids'].str.strip('[]').str.split(',').map(np.array)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.