美文网首页
Pandas: 解决concat后索引重复的问题

Pandas: 解决concat后索引重复的问题

作者: wzNote | 来源:发表于2020-05-26 20:10 被阅读0次
import pandas as pd

data1 = {'url':['baidu.com','jianshu.com'],
       'label':['百度','简书']}
data2 = {'url':['google.com'],
       'label':['谷歌']}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# 输出结果
df1:
0    baidu.com    百度
1  jianshu.com    简书

df2:
0  google.com    谷歌

拼接后会发现index重复

df_total = pd.concat([df1, df2])

# 打印结果
0    baidu.com    百度
1  jianshu.com    简书
0   google.com    谷歌

重置索引

df_total = df_total.reset_index(drop=True)

# 打印结果
0    baidu.com    百度
1  jianshu.com    简书
2   google.com    谷歌

重新排序

import numpy as np
df = df_total.reindex(np.random.permutation(df_total.index))

# 打印结果
2   google.com    谷歌
0    baidu.com    百度
1  jianshu.com    简书

相关文章

网友评论

      本文标题:Pandas: 解决concat后索引重复的问题

      本文链接:https://www.haomeiwen.com/subject/aptbahtx.html