Python 从元组到熊猫中的多列
如何转换此数据帧 location value 0 (Richmond, Virginia, nan, USA) 100 1 (New York City, New York, nan, USA) 200 位置值 0(美国南部弗吉尼亚州里士满)100 1(美国南部纽约市纽约市)200 为此: city state region country value 0 Richmond Virginia nan USA 100 1 New York City New York nan USA 200 城市国家区域国家价值 0美国弗吉尼亚州南部里士满100 1美国纽约市纽约南部200Python 从元组到熊猫中的多列,python,pandas,tuples,Python,Pandas,Tuples,如何转换此数据帧 location value 0 (Richmond, Virginia, nan, USA) 100 1 (New York City, New York, nan, USA) 200 位置值 0(美国南部弗吉尼亚州里士满)100 1(美国南部纽约市纽约市)200 为此: city sta
请注意,第一个数据帧中的
location
列包含元组。我想从位置
列中创建四列。如果您返回一系列(拆分)位置,您可以直接将(连接到)结果DF与您的值列合并
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)
addr = ['city', 'state', 'region', 'country']
df[['value']].join(df.location.apply(lambda loc: Series(loc, index=addr)))
value city state region country
0 100 Richmond Virginia NaN USA
1 200 New York City New York NaN USA
我还没有确定时间,但我建议这样做:
df.loc[:,'city']=df.location.map(lambda x:x[0])
df.loc[:,'state']=df.location.map(lambda x:x[1])
df.loc[:,'regions']=df.location.map(lambda x:x[2])
df.loc[:,'country']=df.location.map(lambda x:x[3])
我猜避免显式for循环可能适合于SIMD指令(numpy当然会寻找它,但可能不是其他库)谢谢@exp1orer的解决方案有效,所以我不必尝试你的。做得很好!如果我没有弄错的话,它只有在df
的索引是范围(len(df))
时才起作用,对吗?@exp1orer不太合适join
是索引上与两个帧合并的简写,因此索引只需保持一致(此处将保持一致,因为应用和列选择不会影响索引)。我会编辑答案。应该早就考虑过这一点,但值得注意的是,这比@exp1orer在更大框架上的解决方案要慢得多,因为所有系列都在创建中。