我想从dataframe列中的内容创建唯一的行标识符,以代替索引列
例如
import pandas as pd
from pprint import pprint
df = pd.DataFrame(columns=["ID", "Animal", "Weight", "Description"])
df["ID"] = ["Qw9457", "gft878"]
df["Animal"] = ["Mouse", "Lion"]
df["Weight"] = [20, 67]
df["Description"] = ["hsdg rie", "gtre sjdhi"]
pprint(df)
Output:
ID Animal Weight Description
0 Qw9457 Mouse 20 hsdg rie
1 gft878 Lion 67 gtre sjdhi
我更喜欢使用其余列中的内容重命名索引列, 例如:
df.index = ["MQwrie", "Lgfgt"]
我想知道是否有很好的方法来编程生成 列内容的行标识符(即索引列)
如果希望根据每列中的数据位生成索引,可以使用串行操作将其拼凑在一起,然后分配索引。下面,我们使用动物名字的第一个字母、体重和描述的第一个单词作为新索引
编辑: 是的,如果添加当前行号(从零开始),则可以使用:
相关问题 更多 >
编程相关推荐