我对熊猫不太熟悉,所以这可能是个愚蠢的问题。我试着分析以下数据:
df = pd.DataFrame({
'Country' : ['country1', 'country2', 'country3', 'country4'],
'Industry' : ['industry1:\$20 \n industry4:\$30',
'industry10:\$100',
'industry3:\$2 \n industry4:\$30 \n industry12:\$10 \n industry1:\$3',
'industry1:\$20 \n industry4:\$30'
],})
(\n来自excel提取)
我需要将行业作为指数,将国家作为列。我的直觉是,我需要先对包含多种信息的细胞进行某种“数据解包”,但我不知道如何在熊猫身上这样做。在
谢谢大家。下面有一些答案很有效。我继续搜索,发现了一些与这个问题相关的帖子(有人把这个问题称为“爆炸熊猫行”)。在下面的线程中,有人编写了一个通用函数explode(),它是通用的,性能很好:
Split (explode) pandas dataframe string entry to separate rows
您可以使用:
Industry
的所有列\s+\n\s+
-\s+
表示1个或多个空白Series
重塑形状split
rename
列相关问题 更多 >
编程相关推荐