How do I extract the entire row containing a specific word from a data frame when using Pandas?

Asked 2 years ago, Updated 2 years ago, 87 views

import pandas as pd
a=("Long-term borrowings", "property assets", "short-term borrowings")
b=(1111,3333,5555)
c=(444,7777,899)
d=(345,132,5562)
df=pd.DataFrame(data=(b,c,d),index=a)
print(df)

I just want to extract the value of the row that contains the word borrowing.

pandas dataframe

2022-09-22 17:59

1 Answers

Please refer to the following.

a=("long-term borrowings", "property assets", "short-term borrowings")
b=(1111,3333,5555)
c=(444,7777,899)
d=(345,132,5562)
df = pd.DataFrame(data=(b,c,d),index=a)

df.loc[df.index.str.contains("borrowing")]

             0       1       2
Long-term borrowings 1111 33335555
Short-term borrowings 345 132 5562


2022-09-22 17:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.