如何将Pandas中的对象值转换为字符串值?
我在把这些列里的值变成字符串类型时遇到了问题:['Product', 'Product subtype/Category/Commodity', 'Mode of transportation', 'Origin', 'Destination', 'Port', 'Activity', 'Units']
import pandas as pd
import matplotlib.pyplot as plt
import requests
url = r'https://www.neb-one.gc.ca/open/imports-exports/imports-exports-data-visualization.csv'
df = pd.read_csv(url,encoding='ISO-8859-1')
# Filtering out specific transactional activities and confidential values
clean = (~df.iloc[:,7].isin(['Export / Re-imports' ,'Import / Re-exports','Re-export / Imports' ,'Re-import / Exports']))&(df.iloc[:,-1]!="Confidential")
dfIE = df[clean].copy()
dfIE[['Year','Quarter']] = dfIE['Period'].str.split('Q', expand = True)
dfIE.drop(columns=['Period'], inplace=True)
dfIE.insert(0, 'Year', dfIE.pop('Year'))
dfIE.insert(1, 'Quarter', dfIE.pop('Quarter'))
dfIE['Value'] = pd.to_numeric(dfIE['Value'], errors='coerce')
dfIE['Year'] = pd.to_numeric(dfIE['Year'], errors='coerce')
dfIE['Quarter'] = pd.to_numeric(dfIE['Quarter'], errors='coerce')
我尝试了下面三种方法来把对象的值转换成字符串。
注意:有些值是'NaN',表示不是一个数字。
column_name =[ 'Product', 'Product subtype/Category/Commodity', 'Mode of transportation', 'Origin', 'Destination', 'Port', 'Activity', 'Units']
dfIE[column_name]= dfIE[column_name].astype(str)
dfIE[column_name]= dfIE[column_name].values.astype(str)
dfIE[column_name]= dfIE[column_name].map(str)
1 个回答
0
你需要用 .astype('string')
而不是 .astype(str)
。想了解更多,可以点击 这里。
column_name =['Product', 'Product subtype/Category/Commodity', 'Mode of transportation', 'Origin', 'Destination', 'Port', 'Activity', 'Units']
dfIE[column_name]= dfIE[column_name].astype('string')
df.info()
:
<class 'pandas.core.frame.DataFrame'>
Index: 48582 entries, 0 to 54028
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Year 48582 non-null int64
1 Quarter 48582 non-null int64
2 Product 48582 non-null string
3 Product subtype/Category/Commodity 22682 non-null string
4 Mode of transportation 299 non-null string
5 Origin 19403 non-null string
6 Destination 41902 non-null string
7 Port 5560 non-null string
8 Activity 48582 non-null string
9 Units 48582 non-null string
10 Value 37806 non-null float64
dtypes: float64(1), int64(2), string(8)
memory usage: 4.4 MB