我有csv dataframe-
print(test.loc[1])
outlook sunny
temperature mild
humidity normal
wind weak
playtennis yes
Name: 1, dtype: object
我想将其转换为-
outlook.sunny.temperature.mild.humidity.normal.wind.weak.playtennis.yes
我该如何实现?
最佳答案
让ser = test.loc[1]
。
您可以使用.to_dict()
将此系列转换为字典,
然后使用.items()
将字典转换为键/值元组列表,
然后使用itertools.chain
将元组合并为一个列表,最后
用.join()
将列表项与句点连接起来。
Python代码:
from itertools import chain
'.'.join(chain.from_iterable(ser.to_dict().items()))
#'outlook.sunny.temperature.mild.humidity.normal....yes'
关于python - 如何将pandas loc indexer转换为字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50221920/