我有这个 Dataframe :
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'categories': ['cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7'],
'points': [5, 17, 7, 19, 12, 13, 9, 24]})
我有这个函数来从这个dataframe创建短语:
def conteo_frase(data):
print(f"We have in total {len(df1)} categories")
print(f"---------------------TOP RANKED------------------")
print(f"The most important category is {df.loc[0].categories} with a total of {df.loc[0].points}")
print(f"The 2nd most important category is {df.loc[1].categories} with a total of {data.loc[1].points}")
我想为dataframe中的每一行创建一个注解,并为每一行创建一个for循环。
示例:
We have in total 7 categories
---------------------TOP RANKED------------------
The most important category is cat0 with a total of 5
The 2nd most important category is cat1 with a total of 17
The 3rd most important category is cat2 with a total of 7
but for each row of the dataframe.
1条答案
按热度按时间bejyjqdl1#
你可以在 Dataframe 的每一行使用
apply()
。这个解决方案唯一的警告是你必须单独打印你的头:这就是我们看到的输出:
如果你想阅读更多关于
apply()
这里是链接。