📜  在 Pandas 中将列向左对齐 – Python

📅  最后修改于: 2022-05-13 01:54:47.036000             🧑  作者: Mango

在 Pandas 中将列向左对齐 – Python

Pandas 库可用于在Python中执行探索性数据分析。 Pandas 数据框以表格格式表示数据。我们可以对数据进行操作并显示出来。在本文中,我们将在 Pandas 中将列与左侧对齐。当我们显示数据框时,我们可以将列中的数据对齐为左、右或居中。

默认为右对齐,如下例所示。

Python3
# Python code demonstrate creating
# DataFrame from dict and left aligning
import pandas as pd
 
# initialise data of lists.
data = {'Name' : ['Tania', 'Ravi',
                  'Surbhi', 'Ganesh'],
         
        'Articles' : [50, 30, 45, 33],
         
        'Location' : ['Kanpur', 'Kolkata',
                      'Kolkata', 'Bombay']}
 
# Create DataFrame
df = pd.DataFrame(data)
display(df)


Python3
# Python code demonstrate creating
# DataFrame from dict and left aligning
import pandas as pd
 
# initialise data of lists.
data = {'Name' : ['Tania', 'Ravi',
                  'Surbhi', 'Ganesh'],
         
        'Articles' : [50, 30, 45, 33],
         
        'Location' : ['Kanpur', 'Kolkata',
                      'Kolkata', 'Bombay']}
 
# Create DataFrame
df = pd.DataFrame(data)
 
left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
display(left_aligned_df)


Python3
import pandas as pd
 
# initialise data of lists.
data = [['Raghav', 'Jeeva', 'Imon', 'Sandeep'],
        ['Deloitte', 'Apple', 'Amazon', 'Flipkart'],
        [2,3,7,8]]
 
# Create DataFrame
df = pd.DataFrame(data)
left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
display(left_aligned_df)


Python3
import pandas as pd
 
# initialise data of lists.
data = [['Raghav', 'Jeeva', 'Imon', 'Sandeep'],
        ['Deloitte', 'Apple', 'Amazon', 'Flipkart'],
        [2,3,7,8]]
 
# Create DataFrame
df = pd.DataFrame(data)
left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
 
left_aligned_df = left_aligned_df.set_table_styles(
[dict(selector = 'th', props=[('text-align', 'left')])])
 
display(left_aligned_df)


输出:

为了在 Pandas 数据框中将列向左对齐,我们使用dataframe.style.set_properties()函数。

示例 1:

蟒蛇3

# Python code demonstrate creating
# DataFrame from dict and left aligning
import pandas as pd
 
# initialise data of lists.
data = {'Name' : ['Tania', 'Ravi',
                  'Surbhi', 'Ganesh'],
         
        'Articles' : [50, 30, 45, 33],
         
        'Location' : ['Kanpur', 'Kolkata',
                      'Kolkata', 'Bombay']}
 
# Create DataFrame
df = pd.DataFrame(data)
 
left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
display(left_aligned_df)

输出:

示例 2:

蟒蛇3

import pandas as pd
 
# initialise data of lists.
data = [['Raghav', 'Jeeva', 'Imon', 'Sandeep'],
        ['Deloitte', 'Apple', 'Amazon', 'Flipkart'],
        [2,3,7,8]]
 
# Create DataFrame
df = pd.DataFrame(data)
left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
display(left_aligned_df)

输出:

在上面的例子中,除了列标题之外,所有列的内容都是左对齐的。列标题居中对齐。

示例 3:

如果我们希望列标题左对齐,我们使用set_table_styles()函数

蟒蛇3

import pandas as pd
 
# initialise data of lists.
data = [['Raghav', 'Jeeva', 'Imon', 'Sandeep'],
        ['Deloitte', 'Apple', 'Amazon', 'Flipkart'],
        [2,3,7,8]]
 
# Create DataFrame
df = pd.DataFrame(data)
left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
 
left_aligned_df = left_aligned_df.set_table_styles(
[dict(selector = 'th', props=[('text-align', 'left')])])
 
display(left_aligned_df)

输出:

在上面的例子中,列标题和所有列的内容都是左对齐的。