Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify DataFrame column name in Pandas

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to modify the DataFrame column name in Pandas, the article is very detailed, has a certain reference value, interested friends must read it!

Enter:

$a $b $c $d $e0 1 2 3 4 5

Expected output:

A b c d e

0 1 2 3 4 5

Original data DataFrame:

Import pandas as pd df = pd.DataFrame ({'$averse: [1],'$baked: [2],'$cached: [3],'$dashed: [4],'$eBay: [5]}) solution 1: modify 1 through the properties of the DataFrame.columns class. Brute force modification df.columns = ['averse,' baked, 'cached,' dashed,'e'] 2. Stirp method

The strip () method is used to remove the character (default is a space or newline character) or character sequence specified at the beginning and end of the string.

Df.columns = df.columns.str.strip ('$') 3. Lambda expression

Map () maps the specified sequence based on the function provided. The function function is called with each element in the argument sequence, returning a new list of values returned by each function function.

Lambda x: X [1:] means to take the second element, so the list names $a, $b, and so on only take out a, b.

Df.columns = df.columns.map (lambda x: X [1:]) solution 2: modify 1 through the DataFrame.rename () function. Brute force modification (only part of the column name can be modified) df.rename (columns= ('$averse: 'axiom,' $baked: 'baked,' $cached: 'clocked,' $dashed: 'dashed,' $eforth:'e'}, inplace=True) 2. Lambda expression

Call the replace function to replace $with an empty one.

Df.rename (columns=lambda x:x.replace ('$','), inplace=True) pandas changes the row or column name instance of DataFrame

You can use the rename function to change the row name or column name.

First, build a dataframe:

Import pandas as pdd= {'one': {'afiuzhuo 1, cantilever 2, two': 3, 5, 6, 7, 8, three': (d) print (df) 1234

The output is as follows:

One two three

A 1 5 9

B 2 6 10

C 3 7 11

D 4 8 12

Change column name

Change the name of the second column to twotwo

Df.rename (columns= {'two':'twotwo'}, inplace=True) print (df) 12

The output is as follows:

One twotwo three

A 1 5 9

B 2 6 10

C 3 7 11

D 4 8 12

Change the row name

Change the row names of lines 1 and 2 to aa,bb

Df.rename (index= {'aura) print (df) 12

The output is as follows:

One twotwo three

Aa 1 5 9

Bb 2 6 10

C 3 7 11

D 4 8 12

The change was successful.

Of course, you can also choose to violently change the row or column name:

Df.columns= ['onon','twtw','thth'] print (df) 12

The output is as follows:

Onon twtw thth

Aa 1 5 9

Bb 2 6 10

C 3 7 11

D 4 8 12

The above is all the contents of the article "how to modify the DataFrame list in Pandas". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report