从元组到 pandas 的多列 [英] From tuples to multiple columns in pandas
本文介绍了从元组到 pandas 的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何转换此数据框
location value
0 (Richmond, Virginia, nan, USA) 100
1 (New York City, New York, nan, USA) 200
对此:
city state region country value
0 Richmond Virginia nan USA 100
1 New York City New York nan USA 200
请注意,第一个数据框中的location
列包含元组.我想在location
列之外创建四列.
Note that the location
column in the first dataframe contains tuples. I want to create four columns out of the location
column.
推荐答案
new_col_list = ['city','state','regions','country']
for n,col in enumerate(new_col_list):
df[col] = df['location'].apply(lambda location: location[n])
df = df.drop('location',axis=1)
这篇关于从元组到 pandas 的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文