<p>或者,您可以使用多级索引:</p>
<pre class="lang-py prettyprint-override"><code>Python 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.2 An enhanced Interactive Python. Type '?' for help.
In [1]: import pandas as pd
In [2]: df = pd.DataFrame([['u68472','PO Box 354',None,'LOREDO','Mailing'],^M
...: ['u68472','154 Cedar Park','Apt. 39','LOREDO','Residential']], ^M
...: columns = ['user_id','AddressLine1','AddressLine2','City','address_type'])
In [3]: df
Out[3]:
user_id AddressLine1 AddressLine2 City address_type
0 u68472 PO Box 354 None LOREDO Mailing
1 u68472 154 Cedar Park Apt. 39 LOREDO Residential
In [4]: new_df = df.set_index(['user_id', 'address_type']).unstack('address_type')
In [5]: new_df
Out[5]:
AddressLine1 AddressLine2 City
address_type Mailing Residential Mailing Residential Mailing Residential
user_id
u68472 PO Box 354 154 Cedar Park None Apt. 39 LOREDO LOREDO
In [6]: new_df.columns = new_df.columns.get_level_values(0).str.cat(new_df.columns.get_level_values('address_type').values, sep='_')
In [7]: new_df
Out[7]:
AddressLine1_Mailing AddressLine1_Residential AddressLine2_Mailing AddressLine2_Residential City_Mailing City_Residential
user_id
u68472 PO Box 354 154 Cedar Park None Apt. 39 LOREDO LOREDO
</code></pre>