How Can We Define Columns Names When I Create The Data Frame With Pandas?

In this post we will get to know How can we define Columns names when I create the data frame with pandas which we can perform using the parameter column of the function pd.dataframe()


Columns Names

Here we have given the example of defining the name of the column in pandas using the example below to implement the same and get it done for understanding the same in a better way and learning more.

import pandas as pd

# create a dataframe with column names
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'c'], 'C': [0.1, 0.2, 0.3]}, columns=['A', 'B', 'C'])


In the example given above, we have created a datagram that has let three column which have names (‘A’, ‘B’, and ‘C’) Which have defined all the column using the column parameter of the function pd. data frame (). And the result of the same (above code) is given below to understanding better.

   A  B    C
0  1  a  0.1
1  2  b  0.2
2  3  c  0.3

Here the point we need to keep in mind is that in case we do not specify the names of the column we created the names are assigned by default by the interpreter and the names are as per the law of numbering the column as for example look at the example given below.

# create a data frame without column names
df = pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'y', 'z'], 'c': [0.1, 0.2, 0.3]})

# For change the column names
df. columns = ['A', 'B', 'C']


Here in the example given above we simply created the data frame and did not provide the names of the column we are using here we simply create the data frame without names so the names of the column we created would be ‘1’, ‘2’, and ‘3’ but as we know we can always change the names of the column which we had done in above code where we provided the names as ‘A’, ‘B’, and ‘C’.

And for getting know the result of this code would be as followed.

   A  B    C
0  1  x  0.1
1  2  y  0.2
2  3  z  0.3

And here we have defined the names of the column that are ‘A’, ‘B’, and ‘C’ which is helpful for a log time as it will make the data clear while displaying the data on the consol as it will make certain lines or separators between them to make it clear



To learn more about How can we define column names when I create the data frame with pandas visit: by stack overflow?

To learn more about MongoDB and tutorials related to it visit: MongoDB Problems And Tutorials

Leave a Comment

%d bloggers like this: