Extend pandas dataframe entries to same length

Related searches

If I have a dataframe that looks something like this:

from pandas import DataFrame as df

data_df = df(data={"Number": [234, 7892, 109736, 8384664088]})

print(data_df)

       Number
0         234
1        7892
2      109736
3  8384664088

How can I quickly extend the entries in the dataframe to be the length of the entry with the maximum length using a particular character? For example, if I use "#" to extend them to something like:

       Number
0  234#######
1  7892######
2  109736####
3  8384664088

Find the max width with str.len and then use str.pad to pad each entry:

max_width = data_df['Number'].astype(str).str.len().max()
data_df['Number'].astype(str).str.pad(side='right', fillchar='#', width=max_width)

add columns different length pandas, DataFrame({'b':np.arange(4)}) print(df1) df b 0 0 1 1 2 2 3 3 Out[38]: a 0 0 1 1 import pandas # Note these columns have 3 rows of values: original = pandas. D'] }) new = pandas.concat([original, additional], axis=1) # Identical: # new if not max_len == la: a.extend(['']*(max_len-la)) if not max_len == lb:� pandas.DataFrame.append¶ DataFrame.append (other, ignore_index = False, verify_integrity = False, sort = False) [source] ¶ Append rows of other to the end of caller, returning a new object.

One way would be using str.ljust to fill the right side of the numbers cast to string with as many # as needed to reach the maximum length:

max_len = len(str(max(data_df.Number)))
data_df['Number'] = data_df.Number.astype(str).str.ljust(max_len, '#')

    Number
0  234#######
1  7892######
2  109736####
3  8384664088

pandas.DataFrame.append — pandas 1.1.1 documentation, A better solution is to append those rows to a list and then concatenate the list with the original DataFrame all at once. Examples. >>> df = pd.DataFrame([[� Pandas dataframe.append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.

Find the length of max number from the column and append #

from pandas import DataFrame as df

data_df = df(data={"Number": [234, 7892, 109736, 8384664088]})

n = len(str(data_df['Number'].max()))

data_df['Number'] = [str(i)+'#'*(n-len(str(i))) for i in data_df['Number']]
print(data_df)

Output:

      Number                                                                                                                         
0  234#######                                                                                                                         
1  7892######                                                                                                                         
2  109736####                                                                                                                         
3  8384664088  

pandas.Series.str.len — pandas 1.1.1 documentation, A Series or Index of integer values indicating the length of each element in the ('one', 'two', 'three')]) >>> s 0 dog 1 2 5 3 {'foo': 'bar'} 4 [2, 3, 5, 7] 5 (one, two,� Pandas Append DataFrame DataFrame.append() pandas.DataFrame.append() function creates and returns a new DataFrame with rows of second DataFrame to the end of caller DataFrame. Example 1: Append a Pandas DataFrame to Another. In this example, we take two dataframes, and append second dataframe to the first. Python Program

You can use

lng = len(str(max(data_df.Number)))
data_df['Number'] = data_df['Number'].map(lambda x: f'{x:#<lng}')

       Number
0  234#######
1  7892######
2  109736####
3  8384664088

Merge, join, and concatenate — pandas 0.20.3 documentation, If True, do not use the index values on the concatenation axis. A useful shortcut to concat are the append instance methods on Series and DataFrame. Can either be column names or arrays with length equal to the length of the DataFrame. Example of get the length of the string of column in a dataframe in python: Create dataframe: ##create dataframe import pandas as pd d = {'Quarters' : ['q1','quar2','quarter3','quarter-4'], 'Revenue':[23400344.567,54363744.678,56789117.456,4132454.987]} df=pd.DataFrame(d) print df

pandas.Series.str.cat — pandas 1.1.1 documentation, If others is not passed, then all values in the Series/Index are concatenated into same length as the calling Series/Index, with the exception of indexed objects� pandas.DataFrame.equals¶ DataFrame.equals (other) [source] ¶ Test whether two objects contain the same elements. This function allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements.

Get the number of rows, columns, elements of pandas.DataFrame Display number of rows, columns, etc.: df.info() The info() method of pandas.DataFrame can display information such as the number of rows and columns, the total memory usage, the data type of each column, and the number of non-NaN elements.

Python: Find indexes of an element in pandas dataframe; Pandas : count rows in a dataframe | all or those only that satisfy a condition; Pandas : Loop or Iterate over all or certain columns of a dataframe; Pandas : 4 Ways to check if a DataFrame is empty in Python; Pandas : Select first or last N rows in a Dataframe using head() & tail() Pandas