current position:Home>The pandas method concat failed to sort while merging multiple CSV files

The pandas method concat failed to sort while merging multiple CSV files

2022-02-03 02:03:45 CSDN Q & A

adopt concat Methods will be multiple csv File merge , Write the result to the new csv Before I file, I use sort_values Method to de reorder a column of data , Then write a new csv, The result seems to be a local sort , There is no global sort

df_merged = pd.concat(df_list) # Merge
df_merged.drop_duplicates(subset=[' Search terms '], inplace=False) # duplicate removal
df_merged.sort_values(by=[' Search frequency ranking '], inplace=False) # Sort

The result of the final ranking is
2,548,828
2,548,857
2,564,574
8,021
8,581
17,110
26,617

The desired result should be like this
8,021
8,581
17,110
26,617
2,548,828
2,548,857
2,564,574




Refer to the answer 1:
import pandas as pdimport numpy as npdata ={
   
   
   
   'A': ['1,995,713',        '1,996,214',        '1,997,537',        '1,999,167',        '10,697',        '100,464',        '107,900',        '111,620',        '117,913',        '118,708']}df = pd.DataFrame(data=data)print(df.sort_values(by='A', key=lambda x: x.str.replace(",",'').astype(np.float)))



Refer to the answer 2:



Refer to the answer 3:

use inplace=True ,df_merged It's sort , use False,df_merged unchanged , But return to the ordered Df




Refer to the answer 4:



Refer to the answer 5:

Do not say ? Use it when you're done inplace=True, Or assign it to a variable




Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030203429380.html

Random recommended