Home > other >  f-string formatting in pandas dataframe
f-string formatting in pandas dataframe

Time:04-13

Let's say 'members.xlsx' include columns corresponding to the members' data such as 'Country', 'Name', 'Job', 'Age', 'Hobby', 'Height', 'Weight', etc. I was able to figure out how to extract 'Name' column of the members whose county is the US as below using pandas.

import pandas as pd
df=pd.read_excel('f:/temporary/members.xlsx') 
df1=df.loc[df['Country']=='US', ['Name']]
df1.to_excel('f:/temporary/US_Names.xlsx')

Now, I want to extract other columns also (e.g., 'Job', 'Age', and 'Hobby') of the members in other countries (e.g., 'Greece', 'India'). What I imagine is to replace 'US' and 'Name' with other strings using f-string which is similar to this (I know that 'df1=' should also be changed but let me handle that later...).

for a in ['US','Greece','India']:
    for b in ['Name','Job','Age','Hobby']:
        df1=df.loc.[df['Country']=={a}, [{b}]]

But in 'df1=df.loc.[df['Country']=={a}, [{b}]]', I have no idea where 'f' should be placed for f-string formatting. Any methods would work even if that is not f-string formatting. Thanks in advance.

Environment: windows 10 64-bit, python 3.10.4, pandas 1.4.1

CodePudding user response:

Use:

for a in ['US','Greece','India']:
    for b in ['Name','Job','Age','Hobby']:
        df1=df.loc[df['Country']==a, b]

If you need not the df of a country with different values be separated, just use:

for a in ['US','Greece','India']:
   df1=df.loc[df['Country']==a, ['Name','Job','Age','Hobby']]

Demonstration:

df=pd.DataFrame({'Name':['ali', 'reza'], 'Country':['Ir', 'Gr']})
a='Ir'
b='Name'
df.loc[df['Country']==a, b]

Output:

0    ali
Name: Name, dtype: object
  • Related