List to string python dataframe

Pandas Convert Column to String Type?

In this article, I will explain how to convert single column or multiple columns to string type in pandas DataFrame, here, I will demonstrate using DataFrame.astype(str) , DataFrame.values.astype(str) , DataFrame.apply(str) , DataFrame.map(str) and DataFrame.applymap(str) methods to covert any type to string type.

1. Quick Examples of Convert Column To String

If you are in a hurry, below are some of the quick examples of how to convert column to string type in Pandas DataFrame. You can apply these to convert from/to any type in Pandas.

Note that map(str) and apply(str) takes less time compared with the remaining techniques.

 # Below are the quick examples # Convert "Fee" from int to string df = df.astype() # Using Series.astype() to convert to string df["Fee"]=df["Fee"].values.astype('string') # Multiple columns string conversion df = pd.DataFrame(technologies) df = df.astype() # Multiple columns string conversion df = pd.DataFrame(technologies) df[[ 'Fee', 'Discount']] = df[['Fee','Discount']].astype(str) # Multiple columns string conversion df["Fee"] = df["Fee"].astype(str) df["Discount"]= df["Discount"].astype(str) # Using apply(str) method df["Fee"]=df["Fee"].apply(str) # Using apply(str) with lambda function df["Fee"] = df["Fee"].apply(lambda x: str(x)) # Using map(str) method df['Fee'] = df["Fee"].map(str) # Convert entire DataFrame to string df=df.applymap(str) # Convert entire DataFrame to string df=df.astype(str) 

Now, let’s see a detailed example. first, create a Pandas DataFrame with a few rows and columns, and execute and validate the results. Our DataFrame contains column names Courses , Fee , Duration , and Discount .

 import pandas as pd import numpy as np technologies= (< 'Courses':["Spark","PySpark","Hadoop","Python","Pandas","Hadoop","Spark"], 'Fee' :[22000,25000,23000,24000,26000,25000,25000], 'Duration':['30day','50days','55days','40days','60days','35day','55days'], 'Discount':[1000,2300,1000,1200,2500,1300,1400] >) df = pd.DataFrame(technologies) print(df) print(df.dtypes) 
 # Output: Courses Fee Duration Discount 0 Spark 22000 30day 1000 1 PySpark 25000 50days 2300 2 Hadoop 23000 55days 1000 3 Python 24000 40days 1200 4 Pandas 26000 60days 2500 5 Hadoop 25000 35day 1300 6 Spark 25000 55days 1400 Courses object Fee int64 Duration object Discount int64 dtype: object 

You can identify the data type of each column by using dtypes .

2. Convert Column to String Type

Use pandas DataFrame.astype() function to convert a column from int to string, you can apply this on a specific column or on an entire DataFrame.

The Below example converts Fee column from int to string dtype. You can also use numpy.str_ or ‘str’ to specify string type.

 # Convert "Fee" from int to string df = df.astype() print(df.dtypes) 
 # Output: Courses object Fee string Duration object Discount int64 dtype: object 

3. Convert Specific Column to String

You can also use Series.astype() to convert a specific column. Since each column on DataFrame is pandas Series, I will get the column from DataFrame as Series and use astype() function. In the below example df.Fee or df[‘Fee’] returns Series object.

 # Using Series.astype() to convert column to string df["Fee"]=df["Fee"].values.astype('string') print(df.dtypes) 
 # Output: Courses object Fee string Duration object Discount int64 dtype: object 

4. Convert Multiple Columns to String

You can also convert multiple columns to string by sending dict of column name -> data type to astype() method. The below example converts column Fee from int to string and Discount from float to string dtype.

 # Multiple columns string conversion df = pd.DataFrame(technologies) df = df.astype() print(df.dtypes) # Multiple columns string conversion df = pd.DataFrame(technologies) df[[ 'Fee', 'Discount']] = df[['Fee','Discount']].astype(str) print(df.dtypes) # Multiple columns string conversion df["Fee"] = df["Fee"].astype(str) df["Discount"]= df["Discount"].astype(str) print(df.dtypes) 
 # Output: Courses object Fee object Duration object Discount object dtype: object 

5. Using DataFrame.apply(str)

You can convert the column “Fee” to a string by simply using DataFrame.apply(str) , for example df[«Fee»]=df[«Fee»].apply(str) .

 # Using apply(str) method df["Fee"]=df["Fee"].apply(str) print(df.dtypes) 
 # Output: Courses object Fee object Duration object Discount int64 dtype: object 

Using apply() with a lambda expression also works in this case. For example df[«Fee»] = df[«Fee»].apply(lambda x: str(x)) .

 # Using apply(str) to lambda function df["Fee"] = df["Fee"].apply(lambda x: str(x)) print(df.dtypes) 

Yields same output as above.

Читайте также:  Css default font style

6. Using Series.map(str)

You can also convert the column “Fee” to a string by using Series.map(str) , for example df[‘Fee’]=df[«Fee»].map(str) .

 # Convert columns to string using map(str) method df['Fee'] = df["Fee"].map(str) print(df.dtypes) 

Yields same output as above.

Note: map(str) and apply(str) takes less time to compare with the remaining techniques.

7. Convert All Columns to Strings

If you want to change the data type for all columns in the DataFrame to the string type, you can use df.applymap(str) or df.astype(str) methods.

 # Convert entire DataFrame to string df=df.applymap(str) print(df.dtypes) # Convert entire DataFrame to string df=df.astype(str) print(df.dtypes) 
 # Output: Courses object Fee object Duration object Discount object dtype: object 

Conclusion

In this article, you have learned how to convert columns to string type in pandas using DataFrame.astype(str) , Series.astype(str) and DataFrame.apply(str) methods. Also, you have learned how to convert to string using DataFrame.map(str) and DataFrame.applymap(str) methods.

References

You may also like reading:

Источник

Как преобразовать список в строку DataFrame в Python

Вы можете использовать следующий синтаксис для преобразования списка в строку DataFrame в Python:

#define list x = [4, 5, 8, 'A ' ' B'] #convert list to DataFrame df = pd.DataFrame(x). T 

И вы можете использовать следующий синтаксис для преобразования списка списков в несколько строк DataFrame:

#define list of lists big_list = [[4, 5, 6, 'B'], [4, 2, 1, 'A'], [12, 4, 8, 'C']] #convert list of lists into DataFrame df = pd.DataFrame(columns=['col1', 'col2', 'col3', 'col4'], data=big_list) 

В следующих примерах показано, как использовать каждую из этих функций на практике.

Пример 1: преобразование списка в строку DataFrame

В следующем коде показано, как преобразовать один список в DataFrame с одной строкой в Python:

import pandas as pd #define list x = [4, 5, 8, 'Mavericks'] #convert list to DataFrame df = pd.DataFrame(x). T #specify column names of DataFrame df.columns = ['Points', 'Assists', 'Rebounds', 'Team'] #display DataFrame print(df) Points Assists Rebounds Team 0 4 5 8 Mavericks 

Пример 2. Преобразование списка списков в несколько строк DataFrame

Следующий код показывает, как преобразовать список списков в DataFrame с несколькими строками в Python:

import pandas as pd #define list of lists big_list = [[6, 7, 12, 'Mavericks'], [4, 2, 1, 'Lakers'], [12, 4, 8, 'Spurs']] #convert list of lists into DataFrame df = pd.DataFrame(columns=['Points', 'Assists', 'Rebounds', 'Team'], data=big_list) #display DataFrame print(df) Points Assists Rebounds Team 0 6 7 12 Mavericks 1 4 2 1 Lakers 2 12 4 8 Spurs 

Мы можем проверить количество строк и столбцов результирующего DataFrame с помощью функции .shape() :

Читайте также:  Свойства классов в html

Это говорит нам о том, что полученный DataFrame имеет 3 строки и 4 столбца.

Источник

Convert Object Data Type to String in pandas DataFrame Column in Python (2 Examples)

TikTok Icon Statistics Globe

In this Python post you’ll learn how to convert the object data type to a string in a pandas DataFrame column.

The page will consist of these contents:

Let’s dive right into the tutorial!

Example Data & Add-On Libraries

We first have to load the pandas library to Python:

import pandas as pd # Load pandas

We’ll also have to construct some data that we can use in the examples below:

data = pd.DataFrame('x1':range(0, 5), # Create pandas DataFrame 'x2':['a', 'b', 'c', 'd', 'e'], 'x3':range(10, 15)>) print(data) # Print pandas DataFrame

table 1 DataFrame convert object data type string pandas dataframe column python

Have a look at the previous table. It shows that our example data consists of five rows and three columns.

Let’s check the data types of the columns in our pandas DataFrame:

print(data.dtypes) # Print data types of columns # x1 int64 # x2 object # x3 int64 # dtype: object

As you can see, the columns x1 and x3 are integers, and the column x2 has the object data type.

This might be surprising, since the column x2 obviously contains character strings.

In the following examples, I’ll explain why this is the case. So keep on reading!

Example 1: astype() Function does not Change Data Type to String

In case we want to change the data type of a pandas DataFrame column, we would usually use the astype function as shown below:

data['x2']= data['x2'].astype(str) # Applying astype function

However, after running the previous Python code, the data types of our columns have not been changed:

print(data.dtypes) # Print data types of columns # x1 int64 # x2 object # x3 int64 # dtype: object

The reason for this is that data types have a variable length. Hence, strings are by default stored as the object data type.

In other words: If a pandas DataFrame column has the object dtype, you can usually consider it as a string.

However, there’s one little workaround that I want to show you in the next example.

Example 2: Define String with Manual Length in astype() Function

In Example 1, I have explained that data types have a variable length, and for that reason, strings are automatically set to the object dtype.

There is usually no reason why you would have to change that data type. However, in this example, I’ll show how to specify the length of a string column manually to force it to be converted to the string class.

To accomplish this, we can specify ‘|S’ within the astype function as shown below. This sets the string length to the maximum string lengths in our DataFrame column (i.e. 1):

data['x2']= data['x2'].astype('|S') # Applying astype function print(data) # Print updated pandas DataFrame

table 2 DataFrame convert object data type string pandas dataframe column python

In Table 2 you can see that we have created an updated version of our pandas DataFrame using the previous Python programming code.

In this new DataFrame, you can see a b in front of the values in the column x2. The b stands for bytes, and you can learn more about this here.

However, let’s check the dtypes of our updated DataFrame columns:

print(data.dtypes) # Print data types of columns # x1 int64 # x2 |S1 # x3 int64 # dtype: object

The column x2 has been converted to the |S1 class (which stands for strings with a length of 1).

Please note that this code is based in this thread on Stack Overflow. In this thread, you can learn more about the method of this example.

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which illustrates the Python programming syntax of this article. You can find the video below:

The YouTube video will be added soon.

In addition to the video, you might read the other tutorials on this homepage. You can find some related tutorials below:

  • Convert Integer to String in pandas DataFrame Column
  • Convert Float to String in pandas DataFrame Column in Python
  • Convert True/False Boolean to String in pandas DataFrame Column
  • Convert pandas DataFrame to NumPy Array in Python
  • Get pandas DataFrame Column as List in Python
  • Get Max & Min Value of Column & Index in pandas DataFrame in Python
  • Check if Column Exists in pandas DataFrame in Python
  • Convert datetime Object to Date Only String in Python
  • Convert pandas DataFrame Column to datetime in Python
  • Handling DataFrames Using the pandas Library in Python
  • The Python Programming Language

Summary: You have learned in this tutorial how to transform the object data type to a string in a pandas DataFrame column in the Python programming language. Please let me know in the comments, in case you have additional questions.

Источник

Оцените статью