pandas常用 *** 作小结

pandas常用 *** 作小结,第1张

# coding:utf-8

import pandas as pd

df = pd.DataFrame()


# 查找
res_df = df.loc[(df['列名1'] == '目标值') & (df['列名2'] != ['目标值'])]


# 部分列去重 保留第一行数据
df = df.drop_duplicates(subset=['列名1','列名2'], keep='first')


# 某列转为数组list
new_list = df['列名'].values.tolist()


list1 = ['a', 'b', 'c', 'd']
list2 = ['n', 'b', 'c', 'e']
# 返回数组:两个数组中数据相同的数据
new_list1 = [x for x in list1 if x in list2]
# 返回数组:只在list1不在list2的数据
new_list2 = [x for x in list1 if x not in list2]


# 数组去重 返回乱序去重后得数组
new_new_list = list(set(list1))

# 数组转列,写入excel
# demo_list = ['']
demo_list= ['a','b','c']
demo_list = pd.Series(demo_list,name='标题名字')
result_table_path = r'C:\test.xlsx'
with pd.ExcelWriter(result_table_path) as writer:
    demo_list.to_excel(writer, sheet_name='sheet表名', index=False)


# demo test
demo_list= ['a', 'a', 'b','c']
result_table_path = r'C:\test.xlsx'
if demo_list.__len__() > 0:
    single_list = list(set(demo_list))
    new_list = pd.Series(single_list, name='标题名字')
    with pd.ExcelWriter(result_table_path) as writer:
        new_list.to_excel(writer,sheet_name='sheet表名', index=False)


# 只取所需列
new_df = df[['列名1', '列名3', '列名6', '列名4']]

df1 = pd.DataFrame()
df2 = pd.DataFrame()
# 重置行索引 常用于 两个dataframe进行join合并
df1.reset_index(drop=True, inplace=True)
df2.reset_index(drop=True, inplace=True)

# 横向合并
merge_df = pd.concat([df1, df2], axis=1, join='outer', ignore_index= True)


# dataframe直接新增一列
df['新增列名'] = 'content'

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/870340.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存