Python table row column

Table objects¶

Table objects are constructed using the add_table() method on Document .

Table objects¶

Proxy class for a WordprocessingML element.

Return a _Column object of width, newly added rightmost to the table.

Return a _Row instance, newly added bottom-most to the table.

Read/write. A member of WD_TABLE_ALIGNMENT or None, specifying the positioning of this table between the page margins. None if no setting is specified, causing the effective value to be inherited from the style hierarchy.

True if column widths can be automatically adjusted to improve the fit of cell contents. False if table layout is fixed. Column widths are adjusted in either case if total column width exceeds page width. Read/write boolean.

Return _Cell instance correponding to table cell at row_idx, col_idx intersection, where (0, 0) is the top, left-most cell.

Sequence of cells in the column at column_idx in this table.

_Columns instance representing the sequence of columns in this table.

Sequence of cells in the row at row_idx in this table.

_Rows instance containing the sequence of rows in this table.

Read/write. A _TableStyle object representing the style applied to this table. The default table style for the document (often Normal Table ) is returned if the table has no directly-applied style. Assigning None to this property removes any directly-applied table style causing it to inherit the default table style of the document. Note that the style name of a table style differs slightly from that displayed in the user interface; a hyphen, if it appears, must be removed. For example, Light Shading — Accent 1 becomes Light Shading Accent 1 .

A member of WD_TABLE_DIRECTION indicating the direction in which the table cells are ordered, e.g. WD_TABLE_DIRECTION.LTR . None indicates the value is inherited from the style hierarchy.

_Cell objects¶

Return a paragraph newly added to the end of the content in this cell. If present, text is added to the paragraph in a single run. If specified, the paragraph style style is applied. If style is not specified or is None , the result is as though the ‘Normal’ style was applied. Note that the formatting of text in a cell can be influenced by the table style. text can contain tab ( \t ) characters, which are converted to the appropriate XML form for a tab. text can also include newline ( \n ) or carriage return ( \r ) characters, each of which is converted to a line break.

Return a table newly added to this cell after any existing cell content, having rows rows and cols columns. An empty paragraph is added after the table because Word requires a paragraph element as the last element in every cell.

Return a merged cell created by spanning the rectangular region having this cell and other_cell as diagonal corners. Raises InvalidSpanError if the cells do not define a rectangular region.

List of paragraphs in the cell. A table cell is required to contain at least one block-level element and end with a paragraph. By default, a new cell contains a single paragraph. Read-only

List of tables in the cell, in the order they appear. Read-only.

The entire contents of this cell as a string of text. Assigning a string to this property replaces all existing content with a single paragraph containing the assigned text in a single run.

A value of None indicates vertical alignment for this cell is inherited. Assigning None causes any explicitly defined vertical alignment to be removed, restoring inheritance.

The width of this cell in EMU, or None if no explicit width is set.

_Row objects¶

Sequence of _Cell instances corresponding to cells in this row.

Return a Length object representing the height of this cell, or None if no explicit height is set.

Return the height rule of this cell as a member of the WD_ROW_HEIGHT_RULE enumeration, or None if no explicit height_rule is set.

Reference to the Table object this row belongs to.

_Column objects¶

Sequence of _Cell instances corresponding to cells in this column.

Reference to the Table object this column belongs to.

The width of this column in EMU, or None if no explicit width is set.

_Rows objects¶

Sequence of _Row objects corresponding to the rows in a table. Supports len() , iteration, indexed access, and slicing.

Reference to the Table object this row collection belongs to.

_Columns objects¶

Sequence of _Column instances corresponding to the columns in a table. Supports len() , iteration and indexed access.

Reference to the Table object this column collection belongs to.

Table of Contents

Источник

Working with DataFrame Rows and Columns in Python

DataFrame Rows And Columns

In this article, let us see how to create table-like structures using Python and to deal with their rows and columns. This would be very useful when we are creating data science applications that would require us to deal with a large collection of data. Let us see how can we execute basic functions such as creating, updating, and deleting rows/columns using Python.

What is a Data Frame?

Python, being a language widely used for data analytics and processing, has a necessity to store data in structured forms, say as in our conventional tables in the form of rows and columns. We use the DataFrame object from the Pandas library of python to achieve this. Internally the data is stored in the form of two-dimensional arrays. Let us learn more about DataFrame rows and columns in this article.

Creating a simple DataFrame

Let us learn to create a simple DataFrame with an example.

import pandas as pd data = < "TotalScore": [420, 380, 390], "MathScore": [50, 40, 45] >#load data into a DataFrame object: df = pd.DataFrame(data) print(df)

Result

TotalScore MathScore 0 420 50 1 380 40 2 390 45

Selectively Printing One Dataframe Column

Let us see how to select the desired column in python. Consider that we have a dataframe as seen in the above case. We can select the desired column by their column.

The above code would just print the values of ‘MathScore’ column.

Adding Columns to a Dataframe in Python

Now, at times, we might want to add some more columns as part of our data gathering. we can add more columns to our data frame by declaring a new list and converting it into a column in the dataframe.

# creating a new list called name. name = ['Rhema', 'Mehreen', 'Nitin'] # Using 'Name' as the column name # and equating it to the list df['Name'] = name # Observe the result print(df)

Output

TotalScore MathScore Name 0 420 50 Rhema 1 380 40 Mehreen 2 390 45 Nitin

Deleting a column

We can use the drop() method in the pandas dataframe to delete a particular column.

# dropping passed columns df.drop(["Name"], axis = 1, inplace = True)

Now the column ‘Name’ will be deleted from our dataframe.

Working With Dataframe Rows

Now, let us try to understand the ways to perform these operations on rows.

Selecting a Row

To select rows from a dataframe, we can either use the loc[] method or the iloc[] method. In the loc[] method, we can retrieve the row using the row’s index value. We can also use the iloc[] function to retrieve rows using the integer location to iloc[] function.

# importing pandas package import pandas as pd # making data frame from csv file data = pd.read_csv("employees.csv", index_col ="Name") # retrieving row by loc method first = data.loc["Shubham"] second = data.loc["Mariann"] print(first, "\n\n\n", second)

In the above code, we are loading a CSV file as a dataframe and assigning the column ‘Name’ as its index value. Later we use the index of the rows to retrieve them.

Creating a Dataframe Row in Python

To insert a new row into our dataframe, we can use append() function, concat() function or loc[] function in the dataframe.

#adding a new row using the next index value. df.loc[len(df.index)] = ['450', '80', 'Disha'] display(df) #using append function new_data = df = df.append(new_data, ignore_index = True) #using concat function concat_data = <'Name':['Sara', 'Daniel'], 'MathScore':[89, 90], 'TotalScore':[410, 445] >df2 = pd.DataFrame(concat_data) df3 = pd.concat([df, df2], ignore_index = True) df3.reset_index() print(df3)

Output

Using loc[] method TotalScore MathScore Name 0 420 50 Rhema 1 380 40 Mehreen 2 390 45 Nitin 3 450 80 Disha Using append() function TotalScore MathScore Name 0 420 50 Rhema 1 380 40 Mehreen 2 390 45 Nitin 3 450 80 Disha 4 465 89 Ripun Using Concat() function TotalScore MathScore Name 0 420 50 Rhema 1 380 40 Mehreen 2 390 45 Nitin 3 450 80 Disha 4 465 89 Ripun 5 410 89 Sara 6 445 90 Daniel

Deleting a Row

We can use the drop() method to delete rows. We have to pass the index value of the row as an argument to the method.

# importing pandas module import pandas as pd # making data frame from csv file data = pd.read_csv("employees.csv", index_col ="Name" ) # dropping passed values data.drop(["Shubham", "Mariann"], inplace = True)

Conclusion

Hence, in this article, we have discussed various ways to deal with rows and columns in python. In general, data frames are two-dimensional structures in Python that we can use to store data and perform various other functions.

Источник

Читайте также:  Работа с sql запросами php
Оцените статью